Jump to content
  • Advertisement
Sign in to follow this  
icecubeflower

reading/writing ints

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

I wanted to learn how to read and write ints to a file so I wrote up this test program:
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

struct Numbers
{
   string name;

   void Write();
   void Read(string strfun);
   void Print();
   int inum[5];
};

int main(int argc, char *argv[])
{
   int ix;
   Numbers array;
   Numbers copy;
   string fun;

   if(argc==1)
   {
      cout<<"Enter name of struct: ";
      cin>>array.name;
      for(ix=0;ix<5;ix++)
         cin>>array.inum[ix];

      array.Write();
   }
   else  //if user passes any arguments whatsoever
   {
      cout<<"Enter name of file to load: ";
      cin>>fun;

      array.Read(fun);
      array.Print();
   }

  return EXIT_SUCCESS;
}

void Numbers::Write()
{
   ofstream outfile;
   int ix;
   int iswitch;

   outfile.open(name.c_str(),ios::out|ios::binary);
   for(ix=0;ix<5;ix++)
   {
      outfile.write((char*)&inum[ix],sizeof(int));
   }
   outfile.close();
}

void Numbers::Read(string strfun)
{
   ifstream infile;
   int ix;

   name=strfun;
   infile.open(name.c_str(),ios::in|ios::binary);
   for(ix=0;ix<5;ix++)
   {
      //what is the difference between the next two lines?
      infile.read(reinterpret_cast<char *>(&inum[ix]),sizeof(int));
      //infile.read((char *)&inum[ix],sizeof(int));
   }
   infile.close();
}

void Numbers::Print()
{
   int ix;

   for(ix=0;ix<5;ix++)
   {
      cout<<ix<<": "<<inum[ix]<<"\n";
   }
}



Well, it works. Is that the best way to do it? Use binary I/O and then write every int as 4 bytes and read in every int as 4 bytes (sizeof(int))? Is there no way to do it with the << and >> operators? Also what is the difference between: infile.read(reinterpret_cast<char *>(&inum[ix]),sizeof(int)); and: infile.read((char *)&inum[ix],sizeof(int)); I always do the bottom way but the other example used the reinterpret_cast<char *> thing and he put parenthesis around &inum[ix]. Does it matter? Is anything wrong with either way? I'm not a type casting expert.

Share this post


Link to post
Share on other sites
Advertisement

#include <sstream>
#include <fstream>

bool WriteIntToFile(const char* filename, int myInt)
{
std::ofstream file;
std::stringstream ss;
file.open(filename);

if(!file.is_open())
return false;

ss << myInt;
file << ss.str();

file.close()

return true;

}








Not tested, but would that work?

edited: for typos

[Edited by - DaveMS on April 18, 2009 4:40:14 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by icecubeflower
Well, it works. Is that the best way to do it? Use binary I/O and then write every int as 4 bytes and read in every int as 4 bytes (sizeof(int))?

Is there no way to do it with the << and >> operators?


Of course you can do it with those operators. And you can use binary I/O with the console, too, if you so desire. (That might not be as crazy as it sounds. Maybe you're writing a utility such that the user will redirect standard input and standard output to files when calling the program.) Writing to a file stream works just like writing to the console. As for which you *should* use, *in a given situation* (if one were always better, the other wouldn't exist!), the FAQ I linked you in the other thread talks a bit about the pros and cons.

Quote:
Also what is the difference between:
infile.read(reinterpret_cast<char *>(&inum[ix]),sizeof(int));
and:
infile.read((char *)&inum[ix],sizeof(int));


reinterpret_cast is more specific. We prefer to use more specific constructs in programming in general, and thus specifically we prefer to use more specific casts in C++. ;) Having that name there also acts as a bit of a warning (this is the most dangerous of the possible casts in C++), and makes the cast stand out (a C-style cast is almost invisible when you scan over the source code, and practically impossible to search for, too).

Share this post


Link to post
Share on other sites
Your link says << and >> are for uh... what is called? The one that's not binary. They're for that kind of I/O. It says to use read() and write() for binary I/O. So I guess I'll be using read() and write like in my example.

But now apparently I have to worry about big endian and little endian machines and that maybe somewhere there's some twisted computer where sizeof(int) does not equal four bytes.

I'm like... not gonna worry about that yet. Someday if my program gets ran on one of those freak computers then I'll worry about it.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!