John Gordon
6/21/2011 9:43:00 PM
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"