Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #6572 > unrolled thread
| Started by | lolueec <marvee1981@gmail.com> |
|---|---|
| First post | 2011-06-21 14:22 -0700 |
| Last post | 2011-07-04 12:27 -0400 |
| Articles | 9 — 8 participants |
Back to article view | Back to comp.lang.c
writing and copying a text file lolueec <marvee1981@gmail.com> - 2011-06-21 14:22 -0700
Re: writing and copying a text file John Gordon <gordon@panix.com> - 2011-06-21 21:42 +0000
Re: writing and copying a text file Ike Naar <ike@sverige.freeshell.org> - 2011-06-21 21:56 +0000
Re: writing and copying a text file Keith Thompson <kst-u@mib.org> - 2011-06-21 15:52 -0700
Re: writing and copying a text file Ike Naar <ike@sverige.freeshell.org> - 2011-06-22 06:33 +0000
Re: writing and copying a text file Shao Miller <sha0.miller@gmail.com> - 2011-06-21 18:02 -0500
Re: writing and copying a text file Mark Bluemel <mark_bluemel@pobox.com> - 2011-06-22 08:30 +0100
Re: writing and copying a text file Vinicio Flores <vfloreshdz@gmail.com> - 2011-07-03 20:32 -0600
Re: writing and copying a text file lawrence.jones@siemens.com - 2011-07-04 12:27 -0400
| From | lolueec <marvee1981@gmail.com> |
|---|---|
| Date | 2011-06-21 14:22 -0700 |
| Subject | writing and copying a text file |
| Message-ID | <41ff0881-2f1b-4462-8b26-28ca698b180f@a11g2000yqm.googlegroups.com> |
I'm new to C language and I'm trying to copy and write a text file.
Here is my code - can some one explain why it is not working?
#include <stdio.h>
int main()
{
int x;
FILE *handle1, *handle2;
handle1=fopen("a text file","r+);
handle2=fopen("a text file to write","w+");
while( (x=fgetc(handle1) != EOF){
fputc(x,handle2); // write to the file
}
fclose(handle1);
fclose(handle2);
}
Regards,
Lolu
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2011-06-21 21:42 +0000 |
| Message-ID | <itr38e$9b7$1@reader1.panix.com> |
| In reply to | #6572 |
In <41ff0881-2f1b-4462-8b26-28ca698b180f@a11g2000yqm.googlegroups.com> lolueec <marvee1981@gmail.com> writes:
> I'm new to C language and I'm trying to copy and write a text file.
> Here is my code - can some one explain why it is not working?
> #include <stdio.h>
> int main()
> {
> int x;
> FILE *handle1, *handle2;
> handle1=fopen("a text file","r+);
> handle2=fopen("a text file to write","w+");
> while( (x=fgetc(handle1) != EOF){
> fputc(x,handle2); // write to the file
> }
> fclose(handle1);
> fclose(handle2);
> }
You've got some syntax errors here. (Surely your compiler told you about
them?)
You're missing a double-quote on this line:
handle1=fopen("a text file","r+);
And you're missing a closing parenthesis in this line:
while( (x=fgetc(handle1) != EOF){
Aside from that the program should work, assuming your platform allows
filenames like "a text file", and assuming that the input file exists
and is readable.
You might want to do some basic errorchecking on your fopen() statements
to make sure that they succeed, or print an error message if they fail.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Ike Naar <ike@sverige.freeshell.org> |
|---|---|
| Date | 2011-06-21 21:56 +0000 |
| Message-ID | <slrn3vfsj024sq.1tm.ike@sverige.freeshell.org> |
| In reply to | #6572 |
On 2011-06-21, lolueec <marvee1981@gmail.com> wrote:
> I'm new to C language and I'm trying to copy and write a text file.
> Here is my code - can some one explain why it is not working?
It would be helpful if you could be more specific.
In what way is it ``not working''?
Anyway, here are a few obvious problems:
> #include <stdio.h>
>
> int main()
Better: main(void)
> {
> int x;
> FILE *handle1, *handle2;
>
> handle1=fopen("a text file","r+);
Why "r+" instead of plain "r" ?
Also note that in your code the closing double-qoute after "r+ is missing.
And it's prudent to check whether fopen succeeded.
> handle2=fopen("a text file to write","w+");
Why "w+" instead of plain "w" ?
Again, check whether fopen succeeded.
> while( (x=fgetc(handle1) != EOF){
The parentheses don't match; most likely one is missing after ``handle1)''
> fputc(x,handle2); // write to the file
> }
>
> fclose(handle1);
> fclose(handle2);
>
> }
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2011-06-21 15:52 -0700 |
| Message-ID | <lnhb7iiyqh.fsf@nuthaus.mib.org> |
| In reply to | #6575 |
Ike Naar <ike@sverige.freeshell.org> writes:
> On 2011-06-21, lolueec <marvee1981@gmail.com> wrote:
>> I'm new to C language and I'm trying to copy and write a text file.
>> Here is my code - can some one explain why it is not working?
>
> It would be helpful if you could be more specific.
> In what way is it ``not working''?
>
> Anyway, here are a few obvious problems:
>
>> #include <stdio.h>
>>
>> int main()
>
> Better: main(void)
It should be "int main(void)".
That's probably what Ike meant, but the form without the "int" is
acceptable to sufficiently old compiler.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Ike Naar <ike@sverige.freeshell.org> |
|---|---|
| Date | 2011-06-22 06:33 +0000 |
| Message-ID | <slrn3vfsj03355.1ls.ike@sverige.freeshell.org> |
| In reply to | #6585 |
On 2011-06-21, Keith Thompson <kst-u@mib.org> wrote: > Ike Naar <ike@sverige.freeshell.org> writes: >> On 2011-06-21, lolueec <marvee1981@gmail.com> wrote: >>> int main() >> >> Better: main(void) > > It should be "int main(void)". > > That's probably what Ike meant, but the form without the "int" is > acceptable to sufficiently old compiler. Yes, it should be ``int main(void)''. Thanks for the correction.
[toc] | [prev] | [next] | [standalone]
| From | Shao Miller <sha0.miller@gmail.com> |
|---|---|
| Date | 2011-06-21 18:02 -0500 |
| Message-ID | <itr4em$3m9$1@dont-email.me> |
| In reply to | #6572 |
On 6/21/2011 4:22 PM, lolueec wrote:
> I'm new to C language and I'm trying to copy and write a text file.
> Here is my code - can some one explain why it is not working?
>
> #include<stdio.h>
>
> int main()
If you don't care about parameters for 'main', please use:
int main(void)
> {
> int x;
> FILE *handle1, *handle2;
>
> handle1=fopen("a text file","r+);
You are missing a quotation mark after 'r+'.
> handle2=fopen("a text file to write","w+");
>
You ought to check 'handle1' and 'handle2' to make sure the files were
actually opened. 'fopen' will return a null pointer upon failure.
> while( (x=fgetc(handle1) != EOF){
Please count your parentheses. You have 3 left and 2 right.
> fputc(x,handle2); // write to the file
You ought to check the return value of 'fputc' to make sure there wasn't
an error while writing.
> }
>
> fclose(handle1);
> fclose(handle2);
>
You forgot to return an 'int' value for 'main'.
> }
>
So how about:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int x;
FILE * handle1, * handle2;
handle1 = fopen("a text file", "r+");
if (!handle1) {
puts("Couldn't open \"a text file\"!");
return EXIT_FAILURE;
}
handle2 = fopen("a text file to write", "w+");
if (!handle2) {
puts("Couldn't open \"a text file to write\"!");
fclose(handle1);
return EXIT_FAILURE;
}
while ((x = fgetc(handle1)) != EOF) {
/* Write to the file */
if (fputc(x, handle2) == EOF) {
puts("Error while writing!");
break;
}
}
fclose(handle1);
fclose(handle2);
return EXIT_SUCCESS;
}
[toc] | [prev] | [next] | [standalone]
| From | Mark Bluemel <mark_bluemel@pobox.com> |
|---|---|
| Date | 2011-06-22 08:30 +0100 |
| Message-ID | <its5m0$hgj$1@dont-email.me> |
| In reply to | #6572 |
On 06/21/2011 10:22 PM, lolueec wrote:
First things first - you probably need to read
<http://www.catb.org/~esr/faqs/smart-questions.html>
> I'm new to C language and I'm trying to copy and write a text file.
> Here is my code - can some one explain why it is not working?
What do you mean by "not working" - the code here doesn't even compile.
If that's your problem, start by reading the errors messages from the
compiler.
> #include<stdio.h>
>
> int main()
> {
> int x;
> FILE *handle1, *handle2;
>
> handle1=fopen("a text file","r+);
> handle2=fopen("a text file to write","w+");
>
> while( (x=fgetc(handle1) != EOF){
> fputc(x,handle2); // write to the file
> }
>
> fclose(handle1);
> fclose(handle2);
>
> }
[toc] | [prev] | [next] | [standalone]
| From | Vinicio Flores <vfloreshdz@gmail.com> |
|---|---|
| Date | 2011-07-03 20:32 -0600 |
| Message-ID | <iur8od$eqn$1@dont-email.me> |
| In reply to | #6572 |
You have to use fopen("a text file", "r")
use "r", no "r+", because if you use "r+" it will create a file and then
will read it, this will fail.
[toc] | [prev] | [next] | [standalone]
| From | lawrence.jones@siemens.com |
|---|---|
| Date | 2011-07-04 12:27 -0400 |
| Message-ID | <a969e8-eg2.ln1@jones.homeip.net> |
| In reply to | #7554 |
Vinicio Flores <vfloreshdz@gmail.com> wrote: > > use "r", no "r+", because if you use "r+" it will create a file and then > will read it, this will fail. Please go reread the fopen spec -- "r+" mode does not create the file. -- Larry Jones What this games needs are negotiated settlements. -- Calvin
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web