Help ! /* copy the file char by char */
Author |
Message |
Noah ClayPol #1 / 6
|
 Help ! /* copy the file char by char */
I've written a code to copy a file1 char by char to another file2. However when I open file2 in vi, all I get is trash. I'm sure I'm doing something really stupid but I can't seem to figure it out. Below is my code and output. #include <stdio.h> #define INPUTFILE "practice.txt" #define OUTPUTFILE "practice.out" int main(void) { int ch; FILE *rfp, *wfp; rfp = fopen(INPUTFILE,"r"); wfp = fopen(OUTPUTFILE,"w"); while (( ch = (fgetc(rfp)) != EOF)) fputc(ch, wfp); fclose(rfp); fclose(wfp); return 0; }
|
Tue, 18 Jan 2005 13:42:42 GMT |
|
 |
Ben Pfaf #2 / 6
|
 Help ! /* copy the file char by char */
Quote:
> rfp = fopen(INPUTFILE,"r"); > wfp = fopen(OUTPUTFILE,"w");
You probably want binary reads and writes, so use "rb" and "wb". Quote: > while (( > ch = (fgetc(rfp)) != EOF))
You have lots of parentheses here, but they're in the wrong places: while ((ch = getc (rfp)) != EOF) Quote: > fputc(ch, wfp);
-- "It wouldn't be a new C standard if it didn't give a new meaning to the word `static'." --Peter Seebach on C99
|
Tue, 18 Jan 2005 13:48:30 GMT |
|
 |
Noah ClayPol #3 / 6
|
 Help ! /* copy the file char by char */
Quote: > I've written a code to copy a file1 char by char to another file2. > However when I open file2 in vi, all I get is trash. I'm sure I'm > doing something really stupid but I can't seem to figure it out. > Below is my code and output. > #include <stdio.h> > #define INPUTFILE "practice.txt" > #define OUTPUTFILE > "practice.out" > int main(void) > { > int ch; > FILE *rfp, *wfp; > rfp = fopen(INPUTFILE,"r"); > wfp = fopen(OUTPUTFILE,"w"); > while (( > ch = (fgetc(rfp)) != EOF)) > fputc(ch, wfp); > fclose(rfp); > fclose(wfp); > return 0; >}}
int main(void) { int ch; FILE *rfp, *wfp rfp = fopen(INPUTFILE,"r"); wfp = fopen(OUTPUTFILE,"w"); while ((ch = (fgetc(rfp)) != EOF)) fputc(ch, wfp); fclose(rfp); fclose(wfp); return 0; Quote: }
|
Tue, 18 Jan 2005 13:47:37 GMT |
|
 |
Emmanuel Delahay #4 / 6
|
 Help ! /* copy the file char by char */
Quote: > I've written a code to copy a file1 char by char to another file2. > However when I open file2 in vi, all I get is trash. I'm sure I'm doing > something really stupid but I can't seem to figure it out. Below is my > code and output. > #include <stdio.h> > #define INPUTFILE "practice.txt" > #define OUTPUTFILE > "practice.out"
This is weird. Don't you want #define OUTPUTFILE "practice.out" Quote: > int main(void) > { > int ch; > FILE *rfp, *wfp; > rfp = fopen(INPUTFILE,"r"); > wfp = fopen(OUTPUTFILE,"w");
For best results, use binary mode "rb" abd "wb". Also, you should test the values returned by fopen(). NULL means problem. Quote: > while (( > ch = (fgetc(rfp)) != EOF)) > fputc(ch, wfp); > fclose(rfp); > fclose(wfp); > return 0; > }
Sounds correct to me. I'm testing your code... -- -ed- emdel at noos.fr FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ C-library: http://www.dinkumware.com/htm_cl/index.html "Mal nommer les choses c'est ajouter du malheur au monde." Albert Camus.
|
Tue, 18 Jan 2005 13:59:13 GMT |
|
 |
Noah ClayPol #5 / 6
|
 Help ! /* copy the file char by char */
Quote:
>> rfp = fopen(INPUTFILE,"r"); >> wfp = fopen(OUTPUTFILE,"w"); > You probably want binary reads and writes, so use "rb" and "wb". >> while (( >> ch = (fgetc(rfp)) != EOF)) > You have lots of parentheses here, but they're in the wrong > places: > while ((ch = getc (rfp)) != EOF) >> fputc(ch, wfp);
Fixing the parentheses fixed it. Thanks
|
Tue, 18 Jan 2005 13:57:44 GMT |
|
 |
Emmanuel Delahay #6 / 6
|
 Help ! /* copy the file char by char */
Quote: >> while (( >> ch = (fgetc(rfp)) != EOF)) >> fputc(ch, wfp);
actually, this is weird, and causes trouble. Try that instead: while ((ch = fgetc(rfp)) != EOF) { fputc(ch, wfp); } -- -ed- emdel at noos.fr FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ C-library: http://www.dinkumware.com/htm_cl/index.html "Mal nommer les choses c'est ajouter du malheur au monde." Albert Camus.
|
Tue, 18 Jan 2005 14:04:56 GMT |
|
|
|