• ### Announcements

#### Archived

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

# Basic File IO

## Recommended Posts

I''m in the current process of making a program with C++ in which I would need a class instance for every person whose name I input. Now, I don''t want to have to put 40 peoples names in every time I want to run the program. I want to export all the data to a file, a file of any type, even a .txt, and be able to save and load data from it at any time. I don''t even know where to start. I understand at most the basic concept of how loading and data works logically, but through code I''m clueless. I''ve seen certain functions like fread and fopen, and understand them a little bit (a brief explanation might help), but other than that I have no idea where to begin with file input/output(I checked the Articles and Resources section, but after a few minutes of recieving that Radeon add every time I loaded the pages, I decided I''d do better in the forums). If anyone could point out very basic, very simple tutorials (I''ve found tutorials, but... they were above my head) or, if the process is simple, could explain it to me, or even give me a basic example, I would be thankful.

##### Share on other sites
quote:
Original post by Drizzt DoUrden
I''m in the current process of making a program with C++ in which I would need a class instance for every person whose name I input.

Now, I don''t want to have to put 40 peoples names in every time I want to run the program. I want to export all the data to a file, a file of any type, even a .txt, and be able to save and load data from it at any time.

I don''t even know where to start. I understand at most the basic concept of how loading and data works logically, but through code I''m clueless. I''ve seen certain functions like fread and fopen, and understand them a little bit (a brief explanation might help), but other than that I have no idea where to begin with file input/output(I checked the Articles and Resources section, but after a few minutes of recieving that Radeon add every time I loaded the pages, I decided I''d do better in the forums).

If anyone could point out very basic, very simple tutorials (I''ve found tutorials, but... they were above my head) or, if the process is simple, could explain it to me, or even give me a basic example, I would be thankful.

click on For Beginners on top and if that doesn''t do it, check the Articles and Resources.

[Cyberdrek | the last true sorcerer | Spirit Mage - mutedfaith.com]

##### Share on other sites
Been there done that. I have, within the last 20 minutes, taught myself how to write text to .txt files ^_^. (Sams Teach Yourself C++ in 21 Days had a little section I missed). But I've got it figured out now, it's actually very easy ^_^ no wonder there aren't any tutorials.

Thanks for your help though, sorry for such a frequent post.

[edited by - Drizzt DoUrden on October 21, 2002 3:44:52 PM]

##### Share on other sites
I've honed my skills as a .txt file writer and managed to produce nice .txt files, but it's come to my attention that that's not always what I'll need.

How exactly do you store data (ex. class data, variables) in a certain type of file?

[edited by - Drizzt DoUrden on October 21, 2002 8:04:25 PM]

##### Share on other sites
here is a tutorial on c++ file io... check out the stuff about binary files...

Thanks!

##### Share on other sites
Once again I thank you but even after reading the entire page I found nothing on actaully writing data from a class directly to a file of any type.

##### Share on other sites
quote:
Original post by Drizzt DoUrden
Once again I thank you but even after reading the entire page I found nothing on actaully writing data from a class directly to a file of any type.

Class data, at some level, eventually is simple data of intrinsic type (int, char, long, double, etc). At that point, you simply wish to write the data to/read the data from the file (either as ASCII text using formatted IO, or as binary data using unformatted IO functions):

  class X{public:  void write( std::string filename ) const;  void dump( std::string filename ) const; private:  char c;  double d;  float f;  int i;  long l;  long double ld;  short s;}; void X::write( std::string filename ) const{  std::ofstream fout;  fout.open( filename.c_str() );  if( fout.fail() )  {    // error handling here    return;  }   // formatted IO:  fout << c << '' '' << d << '' '' << f << '' '' << i << '' '' << l       << '' '' << ld << '' '' << s << '' '' << endl;} void X::dump( std::string filename ) const{  std::ofstream fout;  fout.open( filename.c_str() );  if( fout.fail() )  {    // error handling here    return;  }   // unformatted IO:  fout.write( c, sizeof( c ) );  fout.write( d, sizeof( d ) );  fout.write( f, sizeof( f ) );  fout.write( i, sizeof( i ) );  fout.write( l, sizeof( l ) );  fout.write( ld, sizeof( ld ) );  fout.write( s, sizeof( s ) );}

##### Share on other sites
EDIT: I'm an idiot and it works right.

Thanks for helping me out guys ^_^ btw how have you been Oluseyi? Long time no se... err hear.

[edited by - Drizzt DoUrden on October 22, 2002 5:28:41 PM]

##### Share on other sites
BTW -- if you have any problems with Stream IO stuff misbehaving (which is common due to strange stream lameness), use C IO:

  FILE * f = fopen("myfile.dat", "wb+"); // write, binary, with read access (I always use +)fwrite("hello", 1, 6, f);fprintf(f, "Hey %s!\n", your_name);//fseek moves in the file//ftell returns where you are in the filefclose(f); // closes.  you NEED to do this.

There are another set of functions that Win32 API introduces:

CreateFile, ReadFile, WriteFile, etc. They take a lot more parameters to get started, but have a bit better performance.

##### Share on other sites
fout.write(&className, sizeof(className));

##### Share on other sites
quote:
fout.write(&className, sizeof(className));
Watch out for this one, it will store the binary image of the object straight to the stream. It will not work if you have pointers in the class (or if it contain virtual methods, which in turn are pointers btw).

##### Share on other sites
quote:
Original post by dalleboy
Watch out for this one, it will store the binary image of the object straight to the stream. It will not work if you have pointers in the class (or if it contain virtual methods, which in turn are pointers btw).

Or if the structure is padded for word alignment.

Offtopic: virtual methods aren''t necessarily implemented via vtables. It''s left to the compiler writer how to represent them.

quote:
Original post by Drizzt DoUrden
btw how have you been Oluseyi? Long time no se... err hear.

More like "read" actually . I''ve been fairly good. You? We haven''t seen you around here much recently.

##### Share on other sites
I needed some time to mature ^_^. Instead of spending my time refreshing these forum pages to make crap posts, put people down (when they knew 5 times more than I did), and wasting away life, I decided I should hit the books and maybe I''d accomplish something.

I''ve been programming alot lately - I started a computer programming club at my school, I''m to be the president ^_*.

Right now I''m working on figuring out the WinAPI and graphics API''s that I was never good with. Having expierienced more in programming, the windows API and even DirectX/OpenGL look less intimidating. That and the fact that I''ve completed Algebra and I''m taking geometry ^_^.

Good to see you''re still moderating the Linux forum ^_^ I thought by now nes8bit would have changed it to the Windows forum.

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627661
• Total Posts
2978496

• 10
• 12
• 22
• 13
• 33