Binary File Output?

Started by
1 comment, last by BigReaper 23 years, 1 month ago
hello there, can anybody help me with writing some data into a binary file? here is the code i use:

        ofstream File;
	int StartPos[3];
	x=20,y=15;

	File.open("map.dat",ios::out | ios::binary);

	File.seekp(0);
	File.write((char*)StartPos, sizeof(StartPos));
	StartPos[0]=File.tellp();
	File.write((char*)x, sizeof(x));
	StartPos[1]=File.tellp();
	File.write((char*)y, sizeof(y));
	StartPos[2]=File.tellp();
	File.write((char*)map, sizeof(map));
	File.seekp(0);
	File.write((char*)StartPos, sizeof(StartPos));
	File.close();
 
without the x and y datas it works, but with them, there is an error in the function ''streambuf::xsputn''. Hope you can help me.
Advertisement
While I''m not familiar with the C++ File IO methods, I suspect your problem is that it expects a pointer, you can''t cast a non-pointer into a pointer like you have:

(char *)x

you would have to go:

(char *)&x

but in those function calls, you could probably get away with:

&x

as the parameter.

-Mezz
Ok I think that works, there is no error. but when i try to read the file, it reads the wrong data ...
do you know another method to write and read binary data?

This topic is closed to new replies.

Advertisement