[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

writing and copying a text file

marvee1981

6/21/2011 9:23:00 PM

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
8 Answers

John Gordon

6/21/2011 9:43:00 PM

0

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"

Ike Naar

6/21/2011 9:57:00 PM

0

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

Keith Thompson

6/21/2011 10:52:00 PM

0

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.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Shao Miller

6/21/2011 11:03:00 PM

0

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

Ike Naar

6/22/2011 6:33:00 AM

0

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.

Mark Bluemel

6/22/2011 7:30:00 AM

0

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

First things first - you probably need to read
<http://www.catb.org/~esr/faqs/smart-question...

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

Vinicio Flores

7/4/2011 2:33:00 AM

0

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.

lawrence.jones

7/4/2011 4:28:00 PM

0

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