Skip to main content
GameDev.net gamedev.net
🔒 Locked

simple file i/o

Started by FrigidHelix Nov 19, 2000 at 12:36 PM 1 replies 250+ views
Original Post
FrigidHelix
FrigidHelix
using c/c++ under windows, how would one read in and write out variables of varying sizes from a file. i have tried using the fstream class, but it added stuff to the binary file which was not meant to be there. for example, if the file contained the following data in this order, how would it be read and saved? Please provide source code. char long long char[20] thanks for any help.
Tornado
Tornado
Here (With C++ Streams - fstream):

  
ofstream output;
char first_char=''c'';
long first_long=45;
long second_long=344;
char second_char[20]="Streams are cool!"; // =)

output.open("myfile.dat",ios::out | ios::binary);
output.write((char*)&first_char,sizeof(char));
output.write((char*)&first_long,sizeof(long));
output.write((char*)&second_long,sizeof(long));
output.write((char*)&second_char,sizeof(second_char));
output.close();

// now to read it back...


ifstream input;
char first_char;
long first_long;
long second_long;
char second_char[20];
input.open("myfile.dat",ios::in | ios::binary);
input.read((char*)&first_char,sizeof(char));
input.read((char*)&first_long,sizeof(long));
input.read((char*)&second_long,sizeof(long));
input.read((char*)&second_char,sizeof(second_char));
input.close();




- Goblineye Entertainment
The road to success is always under construction
Goblineye EntertainmentThe road to success is always under construction
ColdFlame
ColdFlame
struct data
{
char a;
int b,c;
char d[20];
}*somedata;

somedata=new data;

void* dataptr=(void*)somedata;

Then use ReadFile and WriteFile with dataptr to read and write easily and in a structured way.

I use Read and WritePrivateProfileString myself, coz I can edit it with notepad (until I write my editor)



Oh well, Whatever, Nevermind
Oh well, Whatever, Nevermind

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.