Reading int's from bin file.

Dec 12, 2013 at 11:14am
So i have been trying to read integers from a .bin file with no success.
I have this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 int main()
{
	ifstream file;
	file.open("integer_numbers.bin", ios_base::binary | ios::in);

	int content;

	if(!file.is_open())
	{
		return 0;
	}
	else
	{
		file.read((char*)&content, 4);
		cout << content;
	}
	return 0;
}

But the file wont even open as it always returns 0 after the first if statement. I have the .bin file in the same directory as the .exe file and i'm currently really confused as to why the file wont open as from looking around on the net this is how you are supposed to open a file for reading.

Thanks in advance for the help :)
Dec 12, 2013 at 12:29pm
You should change your code, so that if file isn't opened, it returns something else - like -1 or 1 - so you know that program have not reached its end and terminated too fast.
Then you would know if program really closed because it could not open file, or just went so fast that you couldn't see it.
Dec 12, 2013 at 12:42pm
I changed so it returned -1 and when running the program it exited with a value of -1. So it looks like it cannot open the file.
Dec 12, 2013 at 12:48pm
Alright i got it to work, instead of having the .bin file in the .exe directory i put it on my desktop and wrote the path to the desktop.
Topic archived. No new replies allowed.