C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-12-2004, 11:57 AM   #1
Adamant Programmer
 
Axpen's Avatar
 
Join Date: Jun 2003
Location: USA
Posts: 42
Binary Files

Hello all, i've got a complicated (to me) question to ask.

This one's about binary files, I really don't get how they work or how to get data from them... Problem is, even if I understood the binary file, what should I get from one even if I use fread() properly, which I don't think I am.

Sorry I really don't think posting code will be helpful since I really don't know enough about fread or binary files to really have a good example.

Just wondering if anyone can help, and just so ya know i've done all the help yourself attempts, google had NO good results!

Thanks for the help, I really appriciate it,
Alex
__________________
The Man With 3 Ears::Oh no better get the huskers

Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

In Case I Forget I Have:
Windows XP
For My 32-bit Questions:
Dev C++ (mainly just use its mingw)
For My 16-bit Questions:
Borland Turbo C++ 1.01
Axpen is offline   Reply With Quote
Old 08-12-2004, 12:10 PM   #2
Registered User
 
Join Date: Apr 2002
Posts: 1,571
http://www.antioffline.com/h/c/ch15.html

If you have questions after reading that, come back and ask away.
__________________
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
MrWizard is offline   Reply With Quote
Old 08-12-2004, 12:12 PM   #3
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,730
Ewww so much bad code in that example.
Thantos is offline   Reply With Quote
Old 08-12-2004, 12:19 PM   #4
Registered User
 
Join Date: Apr 2002
Posts: 1,571
Yea, that is true. However, I felt it would be adequate to give someone an idea of how binary files work. Hopefully he won't take any of the bad habits from the example code.

*Don't use void main as in the example.
*Check your return values. (especially from fopen!!)

Those are the worst things I see immediately aside from the horrible style.
__________________
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
MrWizard is offline   Reply With Quote
Old 08-12-2004, 01:43 PM   #5
Adamant Programmer
 
Axpen's Avatar
 
Join Date: Jun 2003
Location: USA
Posts: 42
Well I do have one question, you can use anything in your struct, right? Another question is why did he use xyz in the struct and only x from the struct, memory fullfillment???
--Added--
Also why did he use fopen("junk","r") and "w" rather than "rb" or "wb"
__________________
The Man With 3 Ears::Oh no better get the huskers

Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

In Case I Forget I Have:
Windows XP
For My 32-bit Questions:
Dev C++ (mainly just use its mingw)
For My 16-bit Questions:
Borland Turbo C++ 1.01

Last edited by Axpen; 08-12-2004 at 02:01 PM. Reason: Added Info
Axpen is offline   Reply With Quote
Old 08-12-2004, 02:14 PM   #6
Registered User
 
Join Date: Apr 2002
Posts: 1,571
Yes, you can put anything you want in your struct. He could've used all the members or just one, it doesn't matter. Not sure why he only filled in the first one. I'm not sure of the standard default mode behavior for fopen but he should have used "rb" and "wb" to be explicit. I think on his compiler the default behavior was probably binary mode.
__________________
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
MrWizard is offline   Reply With Quote
Old 08-12-2004, 02:24 PM   #7
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
> Also why did he use fopen("junk","r") and "w" rather than "rb" or "wb"
Such things are only useful in a DOS/Windows environment, you don't need them in Unix/Linux
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Old 08-12-2004, 02:24 PM   #8
Helper
 
Join Date: Jun 2004
Posts: 255
Quote:
Originally Posted by Axpen
This one's about binary files, I really don't get how they work or how to get data from them...
A binary file is just another file. A text file is different.

Let's try to make it clear.

Basically, a file (or should we say a stream) is a sequence of octets. Nothing less, nothing more.

What makes the difference is the semantic you give to the octets or not.

For example, on a binary stream, each value from 0 to 255 has the same 'no-meaning'.

Once the file has been opened in binary mode "rb" or "wb", you can use either the byte-by-byte functions fgetc() / putc() to read / write to the file or the block functions fread() / fwrite().

The block function has an (address, size) interface to define the block to read / write.

The data is called raw, because it is subject to no interpretation at all by the system.:

"What you read is what you get"
"What you write is what you have"

The semantics of the data in the block is under te responsability of the user. He knows how is organized the stream, the size, type and meaning of the fields etc.

Don't attempt to map a C-structure on such a bloc if you want to write portable code.

Text files are different. They have an internal structure based on lines. The 0-255 values belong to a charset where each values has a meaning. Some are characters (For example, in ASCII, 65 is 'A'), some are control characters (13 is CR, 10 is LF etc).

Depending on the system (yes, its a pity), the line termination sequence can be

LF (Unix, Vax-LF)
CR (Mac, Vax-CR)
CRLF (MS-DOS Windows, Vax-CRLF)
LFCR (Vax-LFCR)

From the C point of view, there is only one character : '\n'. It meants that when I call (assuming 'stream' was opened in "w" mode):
Code:
   fprintf (stream, "hello\n");
it will write respectively: (hexadecimal, assuming ASCII)
Code:
68 65 6C 6C 6F 0A
68 65 6C 6C 6F 0D
68 65 6C 6C 6F 0D 0A
68 65 6C 6C 6F 0A 0D
and reverse (read).

In addition, some systems have a special control character interpreted like and end of file marker.

For example, MS-DOS has the 26 (or Ctrl-Z) character. For these reasons (interpretation) the data is called 'cooked'.
__________________
Emmanuel Delahaye

"C is a sharp tool"
Emmanuel Delaha is offline   Reply With Quote
Old 08-12-2004, 02:32 PM   #9
Helper
 
Join Date: Jun 2004
Posts: 255
Quote:
Originally Posted by Salem
> Also why did he use fopen("junk","r") and "w" rather than "rb" or "wb"
Such things are only useful in a DOS/Windows environment, you don't need them in Unix/Linux
This is not true. It's a common mistake made by the people from the Unix world.

The difference between text and binary is a portable C feature. It happens that in some systems, the difference between a text file and a binary file is nul. Good for them, but it's just a peculiar case. The whole world is not Unixoid. Some are MS-DOS/Windows, Mac, Vax, OS/9 etc. Not to mention the embbeded systems.

Due to portabilty issues, the difference should be considered.
__________________
Emmanuel Delahaye

"C is a sharp tool"
Emmanuel Delaha is offline   Reply With Quote
Old 08-12-2004, 06:59 PM   #10
Adamant Programmer
 
Axpen's Avatar
 
Join Date: Jun 2003
Location: USA
Posts: 42
Much appriciated, I understand the difference between binary and text, however, in binary all the bytes are used, such as 0x00 to 0xff, so how would one find the end of a file, since there are often 0x00 (\0) all over? For example a for loop to read until EOF?
__________________
The Man With 3 Ears::Oh no better get the huskers

Download Helppc by David Jurgens, It's a FANTASTIC Reference!!!

In Case I Forget I Have:
Windows XP
For My 32-bit Questions:
Dev C++ (mainly just use its mingw)
For My 16-bit Questions:
Borland Turbo C++ 1.01
Axpen is offline   Reply With Quote
Old 08-12-2004, 07:20 PM   #11
Registered User
 
linuxdude's Avatar
 
Join Date: Mar 2003
Location: Louisiana
Posts: 926
EOF isn't NULL though
Code:
while(fgets(buffer,sizeof buffer,fp)){
[edit]fgets() return s on success, and NULL on error or when end
of file occurs while no characters have been read.

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A '\0' is stored after the last character in the buffer. /*a clear difference*/
[/edit]

Last edited by linuxdude; 08-12-2004 at 07:22 PM.
linuxdude is offline   Reply With Quote
Old 08-12-2004, 07:53 PM   #12
...
 
kermit's Avatar
 
Join Date: Jan 2003
Posts: 1,190
Quote:
Originally Posted by Axpen
Much appriciated, I understand the difference between binary and text, however, in binary all the bytes are used, such as 0x00 to 0xff, so how would one find the end of a file, since there are often 0x00 (\0) all over? For example a for loop to read until EOF?
You could try the feof() function.

~/
__________________
Got Ed?

sys-sizes

Last edited by kermit; 08-12-2004 at 07:55 PM.
kermit is offline   Reply With Quote
Old 08-12-2004, 08:19 PM   #13
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,730
Quote:
Originally Posted by kermit
You could try the feof() function.

~/
Why it's bad to use feof() to control a loop
Thantos is offline   Reply With Quote
Old 08-12-2004, 08:27 PM   #14
...
 
kermit's Avatar
 
Join Date: Jan 2003
Posts: 1,190
Quote:
Originally Posted by Thantos
edit..

n/m
__________________
Got Ed?

sys-sizes

Last edited by kermit; 08-12-2004 at 08:34 PM.
kermit is offline   Reply With Quote
Old 08-12-2004, 08:41 PM   #15
Helper
 
Join Date: Jun 2004
Posts: 255
Quote:
Originally Posted by Axpen
Much appriciated, I understand the difference between binary and text, however, in binary all the bytes are used, such as 0x00 to 0xff, so how would one find the end of a file, since there are often 0x00 (\0) all over? For example a for loop to read until EOF?
The files are managed with some internal size information that is used to detect the end of the file. The details belong to the system.

From the C point of view, the end of file is a status that is identified by the feof() function, after a read error has been detected by one of the read functions: getc(), fscanf() and fread() returning EOF or fgets() returning NULL.
__________________
Emmanuel Delahaye

"C is a sharp tool"
Emmanuel Delaha is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Linking header files, Source files and main program(Accel. C++) Daniel Primed C++ Programming 3 01-17-2006 11:46 AM
send/recv binary files using sockets in C++ dafatdude Networking/Device Communication 14 07-25-2004 11:00 AM
MFC: CStrings & binary files question(s) BrianK Windows Programming 0 06-24-2004 05:41 PM
Binary files Brian C Programming 2 02-18-2002 01:13 PM
storing string objects to binary files Unregistered C++ Programming 2 10-06-2001 11:33 PM


All times are GMT -6. The time now is 04:26 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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