Problem using fwrite

This is a discussion on Problem using fwrite within the C Programming forums, part of the General Programming Boards category; Hi there, I'm trying to use the "fwrite" command to write some values to a data file. However, when I ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Problem using fwrite

    Hi there,

    I'm trying to use the "fwrite" command to write some values to a data file. However, when I tried to read the text (integers) from the data file, the resulting values in the file couldn't be recognised (doesn't look like binaries as well). Do I need some form of conversion?

    I'm new to C programming. Can anyone advise me what I could have done wrong when using the fwrite command?

    I'm using an example to illustrate the way which I have used fwrite to store the values in the file.

    Code:
    int data[4][3] = 
    {1, 2,3
    4,5,6,
    7,8,9,
    10,11,12
    };
    
    size_t obj_size = sizeof(int);
    size_t obj_cnt = 12;
    char *filename= "output.dat";
    FILE *p_file;
    
    p_file=fopen(filename,"w");
    fwrite(&data, obj_size, obj_cnt, p_file);
    fclose (p_file);
    Thanks for any help in advance.

    Ken

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    "w" is for text. "wb" is for binary.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    12
    Hi, I appreciate your quick response.

    I just couldn't read the values using normal editors, even after I replaced "w" to "wt" or "wb". Ideally I'll like to write the values as integers (just like the inputs).

    Can you advise me on what are the changes I should make?

    Thanks for that.

    Ken

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Right now you are writing out the values one through twelve. Since none of the ASCII characters between one and twelve are printable characters, you probably don't see much.

    If you want human-readable output, you should use one of the human-readable output commands (i.e., any output command that isn't fwrite), such as fprintf.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Oh I thought you were trying to write in binary. nevermind.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    This is wrong:
    Code:
    fwrite(&data, obj_size, obj_cnt, p_file);
    Because it writes the address of the array and not the contents of the array.
    Remove the &.
    And remember this rule: when passing an array to a function that expects a pointer, never use &, because an array is, to put it simply, interpreted as type* when passed to functions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Elysia View Post
    This is wrong:
    Code:
    fwrite(&data, obj_size, obj_cnt, p_file);
    Because it writes the address of the array and not the contents of the array.
    Remove the &.
    And remember this rule: when passing an array to a function that expects a pointer, never use &, because an array is, to put it simply, interpreted as type* when passed to functions.
    Come, we've had this argument before: data and &data reference the same memory location.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Here is my output from the program above (after fixing missed comma):

    Code:
    0000000000: 01 00 00 00 02 00 00 00 │ 03 00 00 00 04 00 00 00  ☺   ☻   ♥   ♦
    0000000010: 05 00 00 00 06 00 00 00 │ 07 00 00 00 08 00 00 00  ♣   ♠   •   ◘
    0000000020: 09 00 00 00 0D 0A 00 00 │ 00 0B 00 00 00 0C 00 00  ○   ♪◙   ♂   ♀
    0000000030: 00                      │
    As we see in binary it exactly stores 4 bytes per number with hex values from 1 to 0xc == 12

    So it seems to be working
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    PS. Aaaa, sorry - there is 0D 0a there that shouldn't be - to fix it you have to open file as binary:

    Code:
    p_file=fopen(filename,"wb");
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Quote Originally Posted by tabstop View Post
    Come, we've had this argument before: data and &data reference the same memory location.
    Ehhh, the compiler seems to generate the exact same code for both:
    void* pTest1 = &MyArray;
    004010B0 lea eax,[ebp-6Ch]
    004010B3 mov dword ptr [ebp-78h],eax
    pTest1 = MyArray;
    004010B6 lea eax,[ebp-6Ch]
    004010B9 mov dword ptr [ebp-78h],eax
    But using & on an array is not a good thing, in 99% of the cases.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,603
    Come, we've had this argument before: data and &data reference the same memory location.
    True, but in this case Elysia's point is valid: one wants to pass the array, not the address of the array, even though in the end both point to the same first element of the array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

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