trouble with files
hello,
I am trying to save some data in a file but i can't seem to get it working. I have a list with 40 items of the same class. I want to save all of them in a file.
I used ofstream, but that only seems to support saving char's.
I could do it that way, but all the parameters in the class are private so i'd have to make a function in the class that has acces to these. I tried it, but passing the stream to this function didn't work out too nice.
is there a way to save data different then chars? I had a look at read and write but this also needed char input ( pointer to actually)...
Is there anyone not too shocked (by my methods) to help out?
Quote:Original post by twoaterisn
hello,
I am trying to save some data in a file but i can't seem to get it working. I have a list with 40 items of the same class. I want to save all of them in a file.
I used ofstream, but that only seems to support saving char's.
I could do it that way, but all the parameters in the class are private so i'd have to make a function in the class that has acces to these. I tried it, but passing the stream to this function didn't work out too nice.
is there a way to save data different then chars? I had a look at read and write but this also needed char input ( pointer to actually)...
Is there anyone not too shocked (by my methods) to help out?
You can overload the << operator in your classes so you can output the obbject to an ostream/ofstream.
If you want to output binary data then you use read and write. If you want to write an object you take the address of the object(eg, a pointer) and then cast it to a char pointer. This is because a char is the smallest unit you are able to write with in C++. Here's a tutorial that might help a little:
http://www.codersource.net/cpp_file_io_binary.html
http://www.codersource.net/cpp_file_io_binary.html
Then I would still need the parameters in my class to be public, or am i mistaken?
i'd use this:
ofstream& operator << (ofstream& os, const test& s)
{
return os<<s.parameter1<<'\n'<<s.parameter2<<endl;
}
i'd use this:
ofstream& operator << (ofstream& os, const test& s)
{
return os<<s.parameter1<<'\n'<<s.parameter2<<endl;
}
Quote:Original post by twoaterisn
Then I would still need the parameters in my class to be public, or am i mistaken?
i'd use this:
ofstream& operator << (ofstream& os, const test& s)
{
return os<<s.parameter1<<'\n'<<s.parameter2<<endl;
}
No, you do not need to make your members public, as long as you declare the << operator to be friend of the class.
thanks a lot!
I'll use the tutorial for now, it works for my program. I still need to get the hang of streams and friendships... (aaah, the pun)
I'll use the tutorial for now, it works for my program. I still need to get the hang of streams and friendships... (aaah, the pun)
I got it working half :)
saving the data doesn't work when the file is present in the directory, it does work when i first delete the .sav file and then run the program.
these are my functions:
saving the data doesn't work when the file is present in the directory, it does work when i first delete the .sav file and then run the program.
these are my functions:
void initialiseren(){ fstream binary_file; binary_file.open("chocolade.sav",ios::binary|ios::in); for (int i=0;i<50;i++){ binary_file.read(reinterpret_cast<char *>(&tests),sizeof(test)); } binary_file.close(); }void opslaan() { fstream binary_file; binary_file.open("chocolade.sav",ios::out|ios::binary|ios::app); for (int i=0;i<50;i++){ binary_file.write(reinterpret_cast<char *>(&tests),sizeof(test)); } binary_file.close(); }
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement