problems reading binary file

hello
I'm trying to read the first 6 bytes of a file, but its giving me wired results and i cant seem to figure out what im doing wrong.

this is my code, that reads the file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct Block
{
	char fileSize[3];
	char initialDataBlockId[3];
};

int main(int c, char** a)
{
	
	ifstream file("C\\main_file_cache.idx0", ios::binary);

	Block block;
	
	file.get((char*)&block,sizeof(block));

	printf("File Size: %i\n", block.fileSize);
	printf("Initial Data Block ID: %i\n", block.initialDataBlockId);


	file.close();

	system("pause");
	return 0;
}


before i ran the code, i opened the file in a binary editor,
and it showed me this (hex):
1
2
3
4
00 00 00 00-00 00 05 af-4b 00 00 01-26 df cd 00
00 6f 03 3f-ed 00 03 61-05 08 35 00-04 8b 01 61
59 00 08 39-03 23 0a 00-05 6c 00 35-d0 00 06 fe
03 69 d8 00-07 19


there is a total of 54 bytes and as you can see the first 6 bytes are just 0.

so i expected to my program to output:
1
2
File Size: 0
Initial Data Block ID: 0


but instead it outputted this:
1
2
File Size: 10419128
Initial Data Block ID: 10419131


which makes no sense...
im guessing, maybe, there is something wrong with my code?

could someone help me please? :)
The filename looks suspicious here:
 
    ifstream file("C\\main_file_cache.idx0", ios::binary);

Missing colon here ? "C:\\main... etc.

It is a good idea to check the file status after opening.
Also, check that the read succeeded too.

The printf looks a bit strange too.
Last edited on
You probably want read rather than get.
http://www.cplusplus.com/reference/istream/istream/read/
http://www.cplusplus.com/reference/istream/istream/get/

Get will only read in 5 chars from the file. I'm not exactly sure why get doesn't populate the first 5 bytes, but the fact that the values are all '\0', making the file start with an "empty string" probably has something to do with it.
Duplicate of http://www.cplusplus.com/forum/beginner/218904/

Please DON'T post multiple threads on the same topic. It wastes everyone's time, including your own.
You may find some ideas on converting a 24-bit integer (3 byte array) to an integer here:
https://stackoverflow.com/questions/35876000/converting-24-bit-integer-2s-complement-to-32-bit-integer-in-c

There are a few things to bear in mind - if all the values are unsigned, things are easier (unsigned char, unsigned int).

Also byte order is important, the file may contain low-order byte first, or high-order byte first. You need to be careful that your code matches the type of data in the file. That's sometimes referred to as big-endian versus little-endian, or Motorola/Intel.
Last edited on
One other thing to consider: you're assuming that sizeof(Block) is 6. It might not be due to padding.
Hello stav,

I have been sitting on this since this site had problems last week.

In addition to what Chervil said I believe your biggest problem in in the file C:\\main_file_cache.idx0. This file may not be in the correct format for a binary file.

When I added code to create and write to a new file and then used that file for input the program worked fine.

Other changes I made:

char fileSize[3]; to int fileSize{ 0 }; char initialDataBlockId[3]; to 4 to allow for the "\0" at the end. otherwise all you can store is two characters, unless that is what you want
Changes the printf statements from "%i" to "%d" an "%s" to match what is being output better.

With the changes and the proper input file it works.

Bear in mind you can not use a text editor to create a binary file. It does not work quite the way you might think.

BTW line 7 should read int main(int argc, char *argv[]).

Hope that helps,

Andy
This worked for me with a bitmap file.
1
2
3
4
5
6
7
  file.get((char*)&block, sizeof(block));
  printf("File Size: ");
  for (int i = 0; i < 3; ++i)
    printf("%2X ", block.fileSize[i]);
  printf("Initial Data Block ID: ");
  for (int i = 0; i < 3; ++i)
    printf("%2X ", block.initialDataBlockId[i]);
Topic archived. No new replies allowed.