Jump to content
  • Advertisement

Archived

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

Drizzt DoUrden

Basic File IO

This topic is 5747 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''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 this post


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

Share this post


Link to post
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 this post


Link to post
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?

Thanks in advance.

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 file

fclose(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 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!