GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Feb-2008, 20:14
kenkoh kenkoh is offline
New Member
 
Join Date: Feb 2008
Posts: 7
kenkoh is on a distinguished road

Need help with 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 results (integers) from the data file, the resulting values in the file could not be recognised (doesn't look like binaries as well). Do I need some form of conversion? It doesn't help even after I tried to replace the "w" with "wt" and "wb" in the fopen command.

I'm new to C programming. Can anyone advise me if I'm missing something 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.

CPP / C++ / C 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  
Old 16-Feb-2008, 01:49
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Need help with fwrite


OK, you showed us how you wrote the data. But isn't your problem reading it again? If you post your read program we may be able to tell you what's going on.
__________________

Age is unimportant -- except in cheese
  #3  
Old 16-Feb-2008, 06:45
davis
 
Posts: n/a

Re: Need help with fwrite


You need to enable your compiler warnings and pay attention to them...

CPP / C++ / C Code:
kenkoh.c: In function ‘main’:
kenkoh.c:17: warning: missing braces around initializer
kenkoh.c:17: warning: (near initialization for ‘data[0]’)

Your program, with some minor changes for proper array initialization, writes the values that you expect. (Note that it writes the expected values without the proper array initialization braces, too!)

CPP / C++ / C Code:
#include <stdio.h>


int main()
{
    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 = sizeof(data)/sizeof(int);
    char *filename = "output.dat";
    FILE *p_file;

    p_file = fopen(filename, "w"); // add a 'b' if using Windoze
    if(p_file != NULL)
    {
        fwrite(&data, obj_size, obj_cnt, p_file);
        fclose(p_file);
    }
    return 0;
}


Output:

Code:
$ xxd output.dat 0000000: 0100 0000 0200 0000 0300 0000 0400 0000 ................ 0000010: 0500 0000 0600 0000 0700 0000 0800 0000 ................ 0000020: 0900 0000 0a00 0000 0b00 0000 0c00 0000 ................

...so, when we modify the code to read the data back in:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    unsigned int ui;
    unsigned int uj;
    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 = sizeof(data)/sizeof(int);
    char *filename = "output.dat";
    FILE *p_file;

    p_file = fopen(filename, "w"); // add a 'b' if using Windoze
    if(p_file != NULL)
    {
        fwrite(&data, obj_size, obj_cnt, p_file);
        fclose(p_file);
    }

    memset(data, 0, sizeof( data ));

    p_file = fopen(filename, "r"); // add a 'b' if using Windoze
    if(p_file != NULL)
    {
        fread(&data, obj_size, obj_cnt, p_file);
        fclose(p_file);
        for(ui = 0; ui < obj_size; ui++)
        {
            for(uj = 0; uj < obj_cnt/obj_size; uj++)
            {
                printf( "%d ", data[ui][uj] );
            }
        }
        printf( "\n" );
    }

    return 0;
}


Output:

Code:
$ ./kenkoh 1 2 3 4 5 6 7 8 9 10 11 12


:davis:
  #4  
Old 16-Feb-2008, 06:45
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need help with fwrite


Quote:
Originally Posted by kenkoh
...doesn't look like binaries as well...
Well, what does it look like?

A hexadecimal dump of the file written by your program might look something like:
Code:
$ od -tx1 output.dat 0000000 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 0000020 05 00 00 00 06 00 00 00 07 00 00 00 08 00 00 00 0000040 09 00 00 00 0a 00 00 00 0b 00 00 00 0c 00 00 00

This shows the twelve 4-bite integer values in little-endian order, which is what I would expect on my system.

Regards,

Dave
  #5  
Old 16-Feb-2008, 06:54
davis
 
Posts: n/a

Re: Need help with fwrite


Quote:
Originally Posted by davekw7x
Code:
$ od -tx1 output.dat 0000000 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 0000020 05 00 00 00 06 00 00 00 07 00 00 00 08 00 00 00 0000040 09 00 00 00 0a 00 00 00 0b 00 00 00 0c 00 00 00

Dave, is there a problem with your hex display util? Why does it say "20" at "10" and "40" at "20" ...or am I missing something?


:davis:
  #6  
Old 16-Feb-2008, 07:47
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need help with fwrite


Quote:
Originally Posted by davis
Dave, is there a problem with your hex display util? Why does it say "20" at "10" and "40" at "20" ...or am I missing something?


:davis:

It's "od" (Octal dump: the addresses are in octal)
With the -tx1, the contents are hex.

xxd is obviously less confusing, and would been a better choice for the illustration, but I just have all of these old 20th century habits that predate xxd by a decade or three.

If I were going to use xxd to show byte contents, I would use "xxd -g1", to show the individual bytes (so there would be no question in anyone's mind about endianness):

Code:
$ xxd -g1 output.dat 0000000: 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 ................ 0000010: 05 00 00 00 06 00 00 00 07 00 00 00 08 00 00 00 ................ 0000020: 09 00 00 00 0a 00 00 00 0b 00 00 00 0c 00 00 00 ................

The main point is that with binary files you can't look at them with a "normal" text editor but programmers should have a test plan that allows them to verify file operations.

I would always verify that the file actually contains the expected stuff then, and only then, try to implement the read operations in my application.

Regards,

Dave

Footnote: If you look at the timestamps on our original responses, they are in the same minute. If I had seen yours I wouldn't have posted at all, since yours is more helpful. Oh, well...
  #7  
Old 16-Feb-2008, 08:23
davis
 
Posts: n/a

Re: Need help with fwrite


Quote:
Originally Posted by davekw7x
It's "od" (Octal dump: the addresses are in octal)
With the -tx1, the contents are hex.

Thanks for the clarification Dave. I don't know when/if I may have last used od, but surely I must have at some time in the past and have since forgotten it. Perhaps I aliased it...I don't know, too much water under the bridge.

Code:
$ xxd ./output.dat 0000000: 0000 0001 0000 0002 0000 0003 0000 0004 ................ 0000010: 0000 0005 0000 0006 0000 0007 0000 0008 ................ 0000020: 0000 0009 0000 000a 0000 000b 0000 000c ................

I checked and "od" is available on all my Unix/Linux systems...I guess that I could have as easily have man'd it and not have bothered you. However, now that you mention it, 00, 20, 40 doh! Makes perfect sense


:davis:
  #8  
Old 17-Feb-2008, 21:53
Peter_APIIT Peter_APIIT is offline
Account Disabled
 
Join Date: May 2007
Location: Malaysia
Posts: 478
Peter_APIIT can only hope to improve

Re: Need help with fwrite


Quote:
I checked and "od" is available on all my Unix/Linux systems...I guess that I could have as easily have man'd it and not have bothered you. However, now that you mention it, 00, 20, 40 doh! Makes perfect sense

I have no idea what u guys talking here.

As far as i know, we need to verify the binary output as we enter after we have write to it. (Mentioned by dave)

I also a POSIX system supporter.

Do od -tx1 and xxd -g1 is to check the binary file contents after we write to our hard disk ?

What is the problem bother davis ? Sorry for my stupidity.
  #9  
Old 17-Feb-2008, 23:03
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need help with fwrite


Quote:
Originally Posted by Peter_APIIT
Do od -tx1 and xxd -g1 is to check the binary file contents after we write to our hard disk ?

Those command lines display byte-by-byte hex values of file contents for systems that have the utilities that they invoke.

Try "man od"
I believe that all UNIX systems since the very first release (a very long time ago) have this utility. Linux and FreeBSD and WIndows/cygwin installations also have it.

Try "man xxd"

I know for sure that xxd is available for Linux systems and cygwin WIndows systems; don't know about UNICes or other operating systems. Also, even if you don't have cygwin on a Windows system, if you have installed the vim and/or gvim text editor, xxd is part of that distribution.

Inside vim, just enter :%! xxd -g1 and the contents of the currently open file are displayed a byte at a time in hexadecimal. You can actually edit a binary file by changing hex values of any of the bytes. Then enter :%! xxd -r to revert to the normal display before saving the file.

Regards,

Dave
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
using vector or array oshiotse C++ Forum 4 16-Apr-2004 10:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:05.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.