Basic File IO

Started by
12 comments, last by Drizzt DoUrden 21 years, 5 months ago
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.
------------------------------Put THAT in your smoke and pipe it
Advertisement
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]
[Cyberdrek | ]
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]
------------------------------Put THAT in your smoke and pipe it
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?

Thanks in advance.

[edited by - Drizzt DoUrden on October 21, 2002 8:04:25 PM]
------------------------------Put THAT in your smoke and pipe it
here is a tutorial on c++ file io... check out the stuff about binary files...
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Thanks!
------------------------------Put THAT in your smoke and pipe it
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.
------------------------------Put THAT in your smoke and pipe it
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 ) );}  
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]
------------------------------Put THAT in your smoke and pipe it
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.

This topic is closed to new replies.

Advertisement