writing and copying a text file

L

lolueec

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
 
J

John Gordon

In said:
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
}

}

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.
 
I

Ike Naar

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)''
 
K

Keith Thompson

Ike Naar said:
It would be helpful if you could be more specific.
In what way is it ``not working''?

Anyway, here are a few obvious problems:


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.
 
S

Shao Miller

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;
}
 
I

Ike Naar

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.
 
M

Mark Bluemel

On 06/21/2011 10:22 PM, lolueec wrote:

First things first - you probably need to read
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.
 
V

Vinicio Flores

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.
 
L

lawrence.jones

Vinicio Flores said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top