![]() |
|
#1
|
|||
|
|||
Need help with fwriteHi 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:
Thanks for any help in advance. Ken |
|
#2
|
||||
|
||||
Re: Need help with fwriteOK, 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
|
|||
|
|||
Re: Need help with fwriteYou need to enable your compiler warnings and pay attention to them...
CPP / C++ / C Code:
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:
Output: Code:
...so, when we modify the code to read the data back in: CPP / C++ / C Code:
Output: Code:
:davis: |
|
#4
|
|||
|
|||
Re: Need help with fwriteQuote:
A hexadecimal dump of the file written by your program might look something like: Code:
This shows the twelve 4-bite integer values in little-endian order, which is what I would expect on my system. Regards, Dave |
|
#5
|
|||
|
|||
Re: Need help with fwriteQuote:
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
|
|||
|
|||
Re: Need help with fwriteQuote:
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:
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
|
|||
|
|||
Re: Need help with fwriteQuote:
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:
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
|
|||
|
|||
Re: Need help with fwriteQuote:
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
|
|||
|
|||
Re: Need help with fwriteQuote:
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 GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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