Jump to content
  • Advertisement
Sign in to follow this  
twoaterisn

trouble with files

This topic is 4519 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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:


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();

}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!