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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a binary file and putting the data into an array

Status
Not open for further replies.

drrep

Programmer
Mar 13, 2002
47
US
Hi everyone,

Ok, I have a binary file which has 400 lines and contains 50 bytes per line.

Problem is of course, the file is in binary and when I open it in a regular text editor, it's all garbage that's displayed. Apparently, that's because, from what I understand, the data is compressed.

How can I uncompress it such that each byte from the 50 byte line displays 8 bits each, thus 0 and 1's so I can put this into an array and continue on?

Is it with the fopen command where I must add "wb", b being for binary that it reads correctly or is there an actual algorithm to transform each byte into bits of 0 or 1?

Any help/ideas would be appreciated...
 
Drrep,

The reason you cant use a regular text editor to view it is because the editor tries to make sense of the bytes by equating it to a ASCII character.

This is not 'compression'.

You can of coz, keep a byte array, open the file in binary format and read it into the array. Simple as that.

The file IS in the format you want i.e. 0's and 1's
You can verify it with a binary editor (you can download one at
best of luck

/Srikanth
In the sweat of thy brow shall you eat your bread.
-Bible
 
in_file=fopen("file.bin", "rb");
fread(&array, 50, 400, in_file);

The term "lines" is applicable to text files only. Binary files have sets of bytes only, no lines.
 
Hmmm ok, so far I get a bit what you guys are suggesting.

But if I declare a byte array (how to is another question?), of size 50 by 400, how will I see how many bits each byte represents?

Like one of you mentionned, I only see the ASCII characters...

Dunno, not that clear to me yet

 
or more like what are the bits that each byte represents?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top