Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to read/write binary data from/to files

Status
Not open for further replies.

samibami

Programmer
Oct 12, 2003
28
0
0
IL
hi there!
i'm looking for the easyest way to read and write binary data to a file.i don't mean the usual "fopen" commands but something else.maybe borland has special commands formats.
thanks a lot!!
 
It's not Borland specific but have you looked at C++ streams?


James P. Cottingham
[sup]
There's no place like 127.0.0.1.
There's no place like 127.0.0.1.
[/sup]
 
You could also look at the VCL's TFileStream - very easy to use.
 
Please share.

I use Delphi quite a bit, and there binary files are easy as long as your types are structured. I've looked at the VCL filestreams, but never quite got it.

I assume that the Delphi VCL filestream and Borland C++ would be very similar.

o__
,_.>/ _
(_)_\(_)_______
..speed is good
 
TFileStream is very easy to use.

Example:
[tt]
// open files
TFileStream*infile=0,*outfile=0;
try
{
infile=new TFileStream("infile.dat",fmOpenRead);
outfile=new TFileStream("outfile.dat",fmCreate);
}
catch(...)
{
delete infile; // closes the file, doesn't delete it!
ShowMessage("Can't open files!");
Close();
}

// copy infile to outfile
const int bufsize=65536;
char*buf=new char[bufsize];
while(int bytesread=infile->Read(buf,bufsize))
outfile->Write(buf,bytesread);

// close the files, free memory
delete infile;
delete outfile;
delete[]buf;
[/tt]


-----
ALTER world DROP injustice, ADD peace;
 
Ok, yeah I see that, and that is easy.

Lets say I am writing a multimedia app.

I want my one big file for my sounds, and my file for my graphics. (I would like to compress it too)

How do I know which bitmap etc. I am accessing?

Or even worse, what if I want to combine the sound, graphics?
 
You can create a header structure in the file that contains information about what's in the file. i.e. you might store how many sound clips are in the file, the sizes of each clip, and properties of each clip. Then use the header information to access items in the file.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top