File operation: Clarification for fwrite.

This is a discussion on File operation: Clarification for fwrite. within the C Programming forums, part of the General Programming Boards category; Hello All, I need some clarification for fwrite. Please note that the below mentioned behaviour is reproducble on GCC X86 ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    2

    Question File operation: Clarification for fwrite.

    Hello All,

    I need some clarification for fwrite.

    Please note that the below mentioned behaviour is reproducble on GCC X86 (rhel 5).
    Code:
    #include<stdio.h>
    
    int main()
    {
     unsigned long writeBuffer [64];
     unsigned long Pos;
     unsigned long j;
    
     FILE* pFile = fopen("tmp.txt", "w+t");
    
     for(j = 0; j < 64; j++)
       writeBuffer[j] = 2560 + j * 4;             // -----------------------(A)
    
     fwrite(writeBuffer, 1, 256, pFile);
    
     fflush(pFile);
    
     fgetpos(pFile, &Pos);
     printf("pos = %d\n", Pos);
    
     fclose (pfile);
    }
    -------------------------------------------
    Expected output : 256
    Actual Output : 320
    ---------------------------------------------

    1) Can i use fwrite even though the file has been opened in text mode?
    If yes, then will using fwrite change the file mode to binary?

    2) i do get the expected output for all the below mentioned cases:
    a) open the file in binary mode.
    b) Change 2560 to 0 in statement [A].
    c) Write an unsigned char instead of an unsigned int.

    I understand that the way data's get stored using fprintf and fwrite are different. But still if i use fwrite and dump 256 bytes of data in to a file, fgetpos should have returned 256?


    Any one can help me with this?

    Thanks,
    Suresh

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sureshreddy.ap View Post
    Hello All,
    1) Can i use fwrite even though the file has been opened in text mode?
    If yes, then will using fwrite change the file mode to binary?
    No.

    2) i do get the expected output for all the below mentioned cases:
    a) open the file in binary mode.
    b) Change 2560 to 0 in statement [A].
    c) Write an unsigned char instead of an unsigned int.
    Look up fwrite() in your documentation...
    it's : fwrite(DataSource,ElementSize,NumberOfElements,Fil eHandle)

    So your call should be: fwrite(WriteBuffer,sizeof(WriteBuffer[0]),64,pFile);

    I understand that the way data's get stored using fprintf and fwrite are different. But still if i use fwrite and dump 256 bytes of data in to a file, fgetpos should have returned 256?
    The problem is that you didn't write 256 bytes...

    Open your file in binary, use fwrite() correctly and it should work.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    2
    Sorry, there was a typo in this statement: it was actually 4, 64.

    Code:
    fwrite(writeBuffer, 4, 64, pFile);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Replies: 3
    Last Post: 11-11-2010, 12:05 PM
  3. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 11:04 AM
  4. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21