Binary Files

Started by
13 comments, last by TridDiute 21 years, 6 months ago
I wonder what is binary files ? i know how to write ASCII files with fstream ... but even if i add ios:binary it writes so i can read it with plain notpad ... ? and when to use binary files ? aint ascii files just as good ?
dudu ...
Advertisement
I''m not really sure either, but I just wrote I stupid file-transfer-over-internet program. When I write the file on the other end, it''s gotta be in binary mode. Otherwise, I can''t open up, say, a transferred bitmap on my XP os. Binary let''s me do it okay.
I would say to use binary when you are dealing with system files or whatever, something that''s not yours. If you are writing files that contain game data, such as stats, use regular ascii, if you want. That''s how I''m doing it right now anyway.
Hope I was more helpful than confusing!

return Neff; //Hi-HO!
i know how to write trasfers over the net with binary ..
i am more taking about creating my own types of binary files:
say a map ... i have herd that its requerd to use binary files for a fast loading sys ...
dudu ...
The difference between ASCII and binary files is found more in the type of data they contain, and also in how that data is stored/retrieved.

ASCII files are more commonly known as ASCII text files, and are generally used to store textual, human-readable information. A great example would be INI files, or the old autoexec.bat and config.sys files that some of us used to deal with. The data is sequentially accessed line-by-line and each line is usually terminated by a CR/LF on windows systems. These types of files are usually intended to be easily createed/edited/viewed by people using a text editor.

Binary files are generally are used to store non-text information, although they can and do store text information as well. An exe is a great example of a binary file. So are database, graphics, and audio files. To get to the data in a binary file, you need to know the file structure. Structures vary widely and are limited only by imagination. Designers either place data at specific locations (byte offsets) in the file, or the file contains some form of index which describes the remainder of the contents. A good example of that is a resource file.
--- "A penny saved never boils."
Passing the std::ios::binary flag disables character translation : Windows (CR/LF), UNIX (LF) and Macs (CR) all have different conventions of how line endings are done. So whenever you write '\n', the compiler replaces it with the line ending appropriate for the platform you are on. This, obviouly, is bad when you want to read/write binary data.

To actually read/write binary data, you need to use std::istream::read, or std::istream:write (among other functions). If you use the formatted I/O overloaded operators (operator<< and operator>>), you will still get ASCII-like output.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]


[edited by - Fruny on October 18, 2002 12:11:43 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote:Original post by RingTek
To get to the data in a binary file, you need to know the file structure. Structures vary widely and are limited only by imagination. Designers either place data at specific locations (byte offsets) in the file, or the file contains some form of index which describes the remainder of the contents. A good example of that is a resource file.


this what i am looking for.
how do i create a file structure ?
and how to use byte offsets ? and create index "headers" ?
a link to a GOOD tutorial on the subject will be nice ..
dudu ...
The easiest way to create a binary file is to write some struct directly to disk using a stream. In order to read that file back in, u simply reverse the process. Stream the data back into your struct.

This requires that the struct you used to write the file is the same EXACT struct you use to read it back in.

Start there - work out the read and write routines using streams, so that you can become familiar with the process.

As far as a tutorial goes, I dont know of any - maybe somebody else does... I learned how to do this by reading/writing PCX files a few years ago. Try searching the web for the file structure for a BMP, and play around with that. You will learn a LOT, believe me!

Hope that helps -

--- "A penny saved never boils."
so i just
fstream::write() to write the struct out ?
can i write classes ?
*and btw thanks for the ansvers : they helped a lot ! *
dudu ...

so i just
fstream::write() to write the struct out ?
can i write classes ?


Ya, that''s the idea - Now, lets say that you knew that an integer value you want is located at byte number 70 and is 4 bytes long. The same concept works, except you would do not have to read the entire file to get that one integer. You only have to position your file stream to the 70th byte and read 4 bytes into your integer variable.

Classes are a different animal, and usually cannot be saved to file without some additional work. You have to be very careful of pointer-referenced variables, as these routines will only save/load the pointer, not the data they reference.

In pascal, example code looks something like this:

type
TMyRecord = record
Name: string[50];
Phone: string[10];
Age: integer;
Registered: boolean;
end;

var
myRecord: TMyRecord;

// Error checking intentionally excluded for clarity

procedure SaveRecord;
var
filestream: TFileStream;
begin
filestream := TFileStream.Create(''test.dat'', fmOpenWrite);
filestream.Write(myRecord, sizeof(TMyRecord));
filestream.Free;
end;

procedure LoadRecord;
var
filestream: TFileStream;
begin
filestream := TFileStream.Create(''test.dat'', fmOpenRead);
filestream.Read(myRecord, sizeof(TMyRecord));
filestream.Free;
end;



--- "A penny saved never boils."
quote:Original post by TridDiute
so i just
fstream::write() to write the struct out ?
can i write classes ?
*and btw thanks for the ansvers : they helped a lot ! *


Not directly, no. You should always write a function that prints out the individual fields of your class, otherwise you will have problems with

- alignment (compilers add padding between the fields, which may vary)
- pointers (you''d save the value of the pointer, not the data)
- objects (you have no idea of their layout)
- virtual functions (they modify the structure of your class)
- base classes (idem, even worse when you have multiple/virtual inheritance)

And more.

You should provide a MyClass::read( std::istream& ); and MyClass::write( std::ostream& ); member functions, acting like std::istream& operator>> ( std::istream&, MyClass& ); and std::ostream& operator<< ( std::ostream&, const MyClass& ); to handle binary output the same way formatted output is.


Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement