[RESOLVED] fwrite gives bad file size when writing value '10'
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] fwrite gives bad file size when writing value '10'

  1. #1
    Join Date
    Jan 2003
    Location
    Sweden
    Posts
    115

    [RESOLVED] fwrite gives bad file size when writing value '10'

    Hello friends

    I have a very peculiar problem in a VS2008 MFC-program. I wrote a function dumping the contents of a 1280*1024*USHORT buffer to a file, but to my surprise the file size was never 2621440 bytes, but around 80-100 bytes more.

    I changed my fwrite:s in all possible fashions, writing all bytes at once, writing one ushort at the time, writing on byte at the time... but problem remained. So then I thought, maybe fwrite recognizes value 10 as the newline characther ('\n') and replaces it by '\r\n'. And, yes, I was correct!

    I did this simple code to trace what happens to all possible ushort values when written to disk using fwrite:

    Code:
    for (USHORT dum=0; dum<=65535; dum++)
    {
    FILE *pTestFile;
    pTestFile = fopen("Logtest.bin", "w+");
    for (int i=0; i<1310720; i++)
       fwrite(&dum, sizeof(USHORT), 1, pTestFile);
    fclose(pTestFile);
    
    pTestFile = fopen("Logtest.bin", "r");
    fseek(pTestFile, 0, SEEK_END);
    int filesize = ftell(pTestFile);
    fclose(pTestFile);
    
    if (filesize != 2621440)
       TRACE("%d %d\n",dum, filesize);
    }
    The output from the TRACE line was:
    10 3932160
    meaning that three bytes had been written for every USHORT. When opening the particular problem file in my hex editor I was not surprised to find the byte sequence '0D 0A 00' , that is '\r\n\0', repeated all through the file, where I would have wanted only '0A 00'.

    This is kind of annoying, when writing a 2.000.000 byte memory block containing random data to a file using one single fwrite operation, I don't want the file to end up containing 2.000.100 bytes just because there happened to be 100 bytes with value 10.

    So, to make the long question short: why does fwrite behave like this, and what can I do in order to avoid that fwrite replaces all 10-values in my buffer with \r\n when writing to the file?

    Thanks in advance!

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,560

    Re: fwrite gives bad file size when writing value '10'

    you should open in binary mode : "wb+"

  3. #3
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: fwrite gives bad file size when writing value '10'

    Your file is probably not opened in binary mode but in text mode. Check your open flags. Also, why are you using C-commands in a C++ environment. For files within MFC you have a CFile object. CFile opens your file in binary by default, so switching to the correct technique will probably automatically fix your problem.

  4. #4
    Join Date
    Jan 2003
    Location
    Sweden
    Posts
    115

    Re: fwrite gives bad file size when writing value '10'

    Quote Originally Posted by Philip Nicoletti View Post
    you should open in binary mode : "wb+"
    But of course, now I feel really stupid! I knew that, I always use "wb" mode, but for some reason I just happend to use "w" this time. Looking at your own code sometimes makes you blind, so I did not spot that.
    Then it takes a second pair of eyes to spot those small things.

    Thank you very much!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center