Advertisement

I/O Newbie

Started by April 05, 2002 07:18 PM
25 comments, last by XtremeIdentity 22 years, 8 months ago
in your dat file, separate your numbers by whitespaces:
60 40 20 36 ... etc
then you can use an ifstream object like this:

int hydra;
ifstream fdata;
fdata.open(filepath);
fdata >> hydra;


since hydra is an int, the ifstream object will start at the beginning of the file and read the first int it sees, 60 in this case. Next time you use fdata>> on a variable, it''ll read the next int (40) and so on. You need to learn more about iostream and the get pointers, go to www.cplusplus.com and look in the reference section for the iostream library.
The downside to the above is that order is critical.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions ]
Thanks to Kylotan for the idea!
Advertisement
Yeah, I''m a beginner too after all. Do you know of a more efficient method?
No, nothing other than <fstream>.

----------------------
Always will be smarter
than you,
--=ME=--
----------------------

[Edit: the board generally interprets <-> enclosed words as attempts at HTML...]

[edited by - Oluseyi on April 13, 2002 3:46:32 AM]
Wachar's Eternity <-<-<-<-<- Me own site!
quote: Original post by SabreMan
Original post by GoofProg
You can. A pointer will be allocated and will point to something or it''s a NULL value

A pointer will *not* be allocated, and an object *cannot* be NULL. The reason the previous code works is that streams define an implicit conversion to bool which gets invoked when performing a boolean check on the stream''s state.

Wow, I stand corrected =) I never knew that, thanks!

The nightmare travels across the cosmos with his burning mane. The trail of ash that is produced.

?Have a nice day!?

It is possible to skip around in files with ifstream. However, you still need to know exactly how many bytes (characters in this case) into the file your data is. You can use the tellg() function to find out where you ara, and the seekg() function to move somewhere else.

Also, I don''t think anyone mentioned this, but you can also separate values in text files with newlines.
Advertisement
Thanks ya''ll!

----------------------
Always will be smarter
than you,
--=ME=--
----------------------
Wachar's Eternity <-<-<-<-<- Me own site!

This topic is closed to new replies.

Advertisement