Thread: Read write problem

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    10

    Read write problem

    I try to read from a file and to write to another file an arbitrary number of bytes

    Code:
    int main (int argc, char* argv[]) 
    
    {
      FILE * pFileR;
      FILE * pFileW;
      char buffer[32];
      pFileR = fopen (argv[1], "rb");
      strcpy (writeFileName, "new_");
      strcat (writeFileName, argv[1]);
      pFileW = fopen (writeFileName, "w");
      if (pFileR==NULL || pFileW==NULL) 
          {
                        if(pFileW!=NULL) fclose(pFileW);
                        if(pFileR!=NULL) fclose(pFileR);
                        fputs ("File error", stderr); 
                        exit (1);
          }
     for(i=0; i<20; i++)
          {
          fread (buffer, 1, sizeof(buffer), pFileR);
          fwrite (buffer, 1, sizeof(buffer), pFileW);
         }
      fclose(pFileR);
      fclose(pFileW);
      return 0;
    }
    The first file is bigger than the number of bytes I read from it. Then I use a file compare utility to compare the files. The problem is that only the first 338 bytes are ok. After that I get an extra space(ASCII) offset and of course the rest of the file is diferent. No matter the size of buffer, I always get only the first 338 bytes correctly written.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    How big is the file?

    You have to remember, on the last 32b chunk that it may not be 32b it could be say 29b (if there are 29b left in the "read" file. And you'll then write 29b + 3b (of rubbish) to the new file and they'll differ. Also if you run out of stuff to read after the first 10 calls, then you'll end up writing 32b of the 10th call (buffer) to the "write" file.

    Check the return result of fread() and call fwrite() accordingly. Perhaps even control your loop in such a way that it's based on the fread() result so you stop reading when there is no more stuff to be read.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you open read file as binary and write file as text - you should use binary mode in both cases
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    10

    Talking

    Writing in text mode was the problem indeed. Now it's ok, the files are indentical as expected. Thanks vart.

    I know I should check the number of bytes read and written. That's the way I wrote the code in the first place but then it gave me this error and I simplified the code in order to find the problem. The problem was that in text mode after 338 bytes a linefeed character was found that was exchanged with a linefeed + carriage return.

    Problem solved, thank you all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read and write binary file?
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 10-29-2008, 05:31 PM
  2. Talking to device over Serial: Write works, Read doesn't
    By theBishop in forum C Programming
    Replies: 2
    Last Post: 07-09-2008, 01:40 PM
  3. Replies: 2
    Last Post: 09-13-2006, 01:14 PM
  4. check for read or write
    By SoFarAway in forum C Programming
    Replies: 6
    Last Post: 04-09-2005, 01:50 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM