|
From: Alan T. <ala...@3n...> - 2001-07-31 22:38:42
|
If I open the file in binary mode, then I get absolutely nothing printed
out. Alan
From: "Lorenzo" <co...@in...>
To: <dev...@li...>
Subject: Re: [Dev-C++] reading bytes
Date: Tue, 31 Jul 2001 09:40:20 +0200
Reply-To: dev...@li...
Try to open file in binary mode using the following code:
ifstream input_file(file, ios::binary);
----- Original Message -----
From: Alan Thomas
To: dev...@li...
Sent: Monday, July 30, 2001 9:29 PM
Subject: reading bytes
I am trying to read bytes from a file. I just read them as characters
and interpret them, since each character takes a byte. The file is a
Microsoft Excel file. However, the bytes look different that expected. The
first few bytes (in decimal) are: 9, 4, 6, 0, 0, 0, 16, 0 instead of
9, 0, 4, 0, 2, 0, 16, 0. Can anyone explain what is happening?
My code and the Excel file format (first few bytes) may be found
below. Thanks, Alan
void main()
{
// Define input & output file names
char *file = "test.xls";
char c;
int byte_no = 0;
// Open input file
ifstream input_file(file);
// read the first byte of file
while (input_file.get(c))
{
if ((byte_no >= 0) && (byte_no <= 7))
cout << endl << "Byte " << byte_no << " is " << short(c) << endl;
byte_no++;
}
// close input file
input_file.close();
system ("PAUSE");
}
Format may be found at: http://www.wotsit.org/search.asp?page=2&s=database
MICROSOFT EXCEL FILE FORMAT
Microsoft Excel is a popular spreadsheet. It uses a file format called BIFF
(Binary
File Format). There are many types of BIFF records. Each has a 4 byte
header. The
first two bytes are an opcode that specifies the record type. The second
two bytes
specify record length. Header values are stored in byte-reversed form (less
significant
byte first). The rest of the record is the data itself (Figure 2-1).
Figure 2-1. BIFF record header.
| Record Header | Record Body
Byte Number | 0 1 2 3 | 0 1 ...
-----------------------------------
Record Contents | XX | XX | XX | XX | XX | XX | ...
-----------------------------------
| opcode | length | data
Each X represents a hexadecimal digit
Two X's form a byte. The least significant (low) byte of the opcode is byte
0 and the
most significant (high) byte is byte 1. Similarly, the low byte of the
record length
field is byte 2 and the high byte is byte 3.
BOF (Beginning of File)
The first record in every spreadsheet is always of the BOF type (Figure
2-2).
Figure 2-2. BOF record.
| Record Header | Record Body |
Byte | 0 1 2 3 | 0 1 2 3 |
-----------------------------------------
Contents | 09 | 00 | 04 | 00 | 02 | 00 | 10 | 00 |
-----------------------------------------
| opcode | length | version | file |
| | | number | type |
The first two bytes, arranged with the low byte first, show that the opcode
for BOF is
09h. The second two bytes indicate that the record body is 4 bytes long.
The first two
bytes of the body are the version number (2 for the initial version of
Excel). The last
two bytes are the file type. Type 10h is a worksheet file.
|