Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Krypto night

file ints

This topic is 5302 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

Ok i know how to read strings and chars from a file. But i have no idia how to input an int into a file and then how to read it again. I have searched every where and cannot find an example or tutorial. Can some one help me out? Tnx in advanced.

Share this post


Link to post
Share on other sites
Advertisement
To text format an integer out to a file and read it back in:

int my_int = something;
// to write:
{
std::ofstream ofs("myfile.dat");
ofs << my_int;
}
// to read:
{
std::ifstream ifs("myfile.dat");
ifs >> my_int;
}

To binary format an integer out to a file and read it back in:

int my_int = something;
// to write:
{
std::ofstream ofs("myfile.dat");
ofs.write(reinterpret_cast<char *>(&my_int), sizeof(my_int));
}
// to read:
{
std::ifstream ifs("myfile.dat");
ifs.read(reinterpret_cast<char *>(&my_int), sizeof(my_int));
}

Share this post


Link to post
Share on other sites

  • 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!