Thread: problem with writing to a file

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    6

    problem with writing to a file

    Hi

    I am new here. Here I was trying to write a program to copy one file to another.

    Code:
    #include <stdio.h>#include <stdlib.h> 
    
    
    int main(int argc, char *argv[])
    
    
    {
        FILE *fp , *ft;
    
    
        if ((fp = fopen(argv[1], "rb")) == NULL)
             {
               printf("Can't open %s for reading\n", argv[1]);
            exit(EXIT_FAILURE);
            }
    
    
        if ((ft = fopen(argv[2], "wb")) == NULL)
             {
               printf("Can't open %s\n", argv[2]);
            exit(EXIT_FAILURE);
            }
    
    
        int ch;
    
    
        while( (feof(fp) == 0) )       
            {  
            fread(&ch, sizeof(int),1, fp);
            
                fwrite(&ch, sizeof(int),1, ft);
                    
            }
    
    
        if (ferror(fp) != 0)
            fprintf(stderr,"Error in reading file %s.\n", argv[1]);
    
    
        if (ferror(ft) != 0)
            fprintf(stderr,"Error in  writing file %s.\n", argv[2]);
    
    
        fclose(ft);
    
    
        fclose(fp);
        
        return 9;
    
    
    }
    This program takes two filenames on the command line. e.g
    ./a.out test1 test2
    .

    So it should copy test1 to test2. Now I created test1 in gedit. I just put one character "c" in it and closed it. And I ran the program. When I check sizes of test1 and test2 , using
    ls -l
    command, I see that test2 has 4 bytes and test1 has 2 bytes.
    So something strange is happening here. I just put a single character in test1, so shouldn't it show 1 byte ? And why test2 is showing 4 bytes ? Any help appreciated. I have Win XP on my laptop. And I use Ubuntu 12 inside VirtualBox. So this is Linux environment...

    thanks
    ratnakar

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    fwrite(&ch, sizeof(int),1, ft);
    You're writing an int each time. That means your "to" file will always be a multiple of sizeof(int), probably 4 or 8 on most systems. Try reading and writing one character at a time:
    Code:
    char ch;
    ...
    fread(&ch, sizeof(ch), 1, fp);
    fwrite(&ch, sizeof(ch), 1, ft);
    Notice, I used ch in my sizeof. That way, whatever type ch is, I read and write only the correct amount of data for that variable.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Oh, and read this link: FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com.

    Note, you don't want to use feof to control your loop. Instead, use the return value of fread. From the documentation:
    Code:
    RETURN VALUE
           fread()  and  fwrite()  return the number of items successfully read or written (i.e., not
           the number of characters).  If an error occurs, or the end-of-file is reached, the  return
           value is a short item count (or zero).
    
    
           fread()  does  not distinguish between end-of-file and error, and callers must use feof(3)
           and ferror(3) to determine which occurred.
    It does suggest you use feof/ferror to check why it didn't read, however, what may not be obvious from that, is you should do it after fread fails. That is, after your loop:
    Code:
    char ch;
    while (fread(&ch, sizeof(ch), 1, fp) != 1)
    {
        if (fwrite(...) != 1)
        {
            // break
        }
    }
    if (ferror(fp))
        // handle error in read file
    if (ferror(ft))
        // handle error in write file
    fwrite has the same return value behavior as fread, so check it's return value too

  4. #4
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    89
    I see that people already replied to this. I just want to add that, for debugging purpose, you could look at the file with a hex editor. That way you can compare the two files and see what's in one file that's not in the other.
    There are a lot of Hex editors out there, I use Ghex (in Ubuntu 12.04).

    Comparison of hex editors - Wikipedia, the free encyclopedia

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    thanks. I didn't get email notification , so I saw this late. I know that char type takes 1 byte at a time, but is 'char' not limited from 0 to 255 ?

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    anduril, I changed int to char in my program and I tried copying a pdf file. I saw the results of two files by 'ls -l' command. I still get 1 more byte in the new file. Why is that ?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Are you still using feof to control the while loop?

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Oh I still am using the feof. Let me read that link posted by anduril. I will post here what I discover

  9. #9
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Ok. I understood the significance of not using feof. Thanks everybody.............

  10. #10
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    You'd be much better off reading and writing as much data as you can in a single call, instead of handling a single byte at a time. Try allocating a large array of char, then make the calls in the following manner:

    Code:
    n = fread(buf, 1, sizeof buf, stream);
    fwrite(buf, 1, n, stream);
    if (n == sizeof buf)
     continue;
    That's usually the fastest way to copy data in standard C. If you only write one byte at a time, then it's more practical just to use getc/putc instead.

  11. #11
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Thanks Barney......will keep that in mind.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem writing to file
    By Imagine in forum C++ Programming
    Replies: 6
    Last Post: 10-28-2007, 08:41 AM
  2. Problem writing to a file!!
    By cs02407 in forum C Programming
    Replies: 2
    Last Post: 07-17-2007, 12:22 AM
  3. Replies: 3
    Last Post: 11-21-2006, 07:26 PM
  4. problem with writing to file
    By Welshy in forum C Programming
    Replies: 3
    Last Post: 03-12-2006, 10:24 AM
  5. Problem writing to file
    By HAssan in forum C Programming
    Replies: 7
    Last Post: 11-05-2005, 09:59 PM

Tags for this Thread