Thread: fputc problem

  1. #1
    Registered User surgeon's Avatar
    Join Date
    Jan 2015
    Posts
    26

    fputc problem

    Greetings. I wrote next program:
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    
        FILE *fp, *fp2;
        int c;
    
        if(argc!=3) {
        printf("Usage:program infile outfile\n");
        return 0;}
    
        else
        fp=fopen(argv[1],"r");
        if(fp==NULL)
            {printf("Error: problem with opening %s\n",argv[1]);
            return 0;}
    
        fp2=fopen(argv[2],"w");
        if(fp==NULL)
            {printf("Error: problem with opening %s\n",argv[2]);
            return 0;}
    
        while(c=fgetc(fp)!=EOF)
        {
        fputc(c,fp2);
        }
    
        printf("Done.\n");
        fclose(fp);
        fclose(fp2);
        
        return 0;
        }
    File _first_: "Hello, try to copy this"

    After running program file _second_ looks like this:
      ЪЪЪЪЪЪЪЪЪЪЪЪЪЪ ЪЪЪЪЪЪЪЪЪЪ

    Help plz.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    This:
    Code:
    while(c=fgetc(fp)!=EOF)
    Is equivalent to:
    Code:
    while(c=(fgetc(fp)!=EOF))
    “c” is being set to either 0 or 1. You want:
    Code:
    while((c=fgetc(fp))!=EOF)

  3. #3
    Registered User surgeon's Avatar
    Join Date
    Jan 2015
    Posts
    26
    Thanks a lot, man! It really helps me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with fputc and fgetc
    By nirnir26 in forum C Programming
    Replies: 5
    Last Post: 12-29-2009, 01:00 PM
  2. fgetc and fputc
    By phoebus in forum C Programming
    Replies: 5
    Last Post: 04-24-2008, 11:23 AM
  3. fputc(int, FILE *) problem
    By kwikness in forum C Programming
    Replies: 11
    Last Post: 12-15-2007, 03:18 PM
  4. putc() != fputc() ?
    By trekker in forum C Programming
    Replies: 2
    Last Post: 07-02-2002, 08:40 AM
  5. fputc(0x0A,file)
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 06:42 AM