Help ! /* copy the file char by char */ 
Author Message
 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  
 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  
 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;

- Show quoted text -

Quote:
}



Tue, 18 Jan 2005 13:47:37 GMT  
 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  
 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  
 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  
 
 [ 6 post ] 

 Relevant Pages 

1. copying unsigned long into char[4] or char*

2. copy char into char[]

3. Converting char array of literals chars to escape chars

4. char *c,char c[ ] and char *c[ ]

5. char, unsigned char, signed char

6. A char pointer (char *) vs. char array question

7. Sorting a Huge Unicode File use strcmp(unsigned char *, unsigned char *)

8. Sorting a Huge Unicode File use strcmp(unsigned char *, unsigned char *)

9. Sorting a Huge Unicode File use strcmp(unsigned char *, unsigned char *)

10. Help explain char * and char [] differences

11. Convert char* to char __gc[]

12. problem assigning char* to char*

 

 
Powered by phpBB® Forum Software