Advertisement

I/O Newbie

Started by April 05, 2002 07:18 PM
25 comments, last by XtremeIdentity 22 years, 7 months ago
I''m an I/O Newbie. Something I swear is messed up with ifstream. Please see if there are any errors. Source Code
  
#include <iostream>
#include <fstream>
using namespace std;

ifstream indata;

int hydralisk;

int main()
	{
	indata.open("hydra.dat");
	indata >> hydralisk;
	cout << hydralisk;
	return 0;
	}
  
hydra.dat hydralisk = 60 Take it to the Xtreme!
--------------------------->Take it to the Xtreme!<---------------------------
quote: Original post by XtremeIdentity




hydra.dat

hydralisk = 60






If that is in the data file, thats the problem. ifstream will read until it reaches white space and put that into the variable its reading from. You dont do ``hydralisk = 60''''. If you want to do it like that, consider using an INI file and the getPrivateProfile*(...) family of functions.


Advertisement
ERROR..ERROR: you are not checking for errors

int main()
{
ifstream hfile;
int mydata;
hfile.open("afile.ext");
if (hfile==NULL)
{
cout<<"OUCH! Something happened"< return 0;
}
//now that it's okay read it in
hfile >> mydata;
cout<return 0;
}

something like that
also make sure that your data file is in the executable's path
I have scratched my head for hours to figure that situation out.



After looking at athe post.. I see what when wrong
Research in the strtok function.. that I'll do it! =)


[edited by - GoofProg on April 5, 2002 8:37:59 PM]

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

?Have a nice day!?

this aint c style crap file io, and you dont check to see if an object that isnt a pointer is NULL

use fstream::is_open() (it returns a bool)
How do I write dat files then?

--------------------------
->Take it to the Xtreme!<-
--------------------------
--------------------------->Take it to the Xtreme!<---------------------------
Do you think C is a crap language.. that hurts =)
Remember that C++ is only C +1.




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

?Have a nice day!?

Advertisement
quote: Original post by XtremeIdentity
How do I write dat files then?


Use your text editor. A .dat file can be anything.

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

?Have a nice day!?

quote: Original post by Anonymous Poster
this aint c style crap file io, and you dont check to see if an object that isnt a pointer is NULL

use fstream::is_open() (it returns a bool)


You can. A pointer will be allocated and will point to something or it''s a NULL value

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

?Have a nice day!?

What''s the proper syntax I mean. Like how would the compiler know this one variable points to this thing?

--------------------------
->Take it to the Xtreme!<-
--------------------------
--------------------------->Take it to the Xtreme!<---------------------------
For fstream a .fail() operator is included. Thats what I always use. Try
if(indata.fail())
cout << "Error opening the file!";

Alos, you might try giving it a specific path name eg. "C:\\Program\\file.dat" or something.

And yes, the double backslashes are needed, it has to do with the escape operators, it will not work with single slashes!

"cogito, ergo sum" -Descartes
"cogito, ergo sum" -Descartes

This topic is closed to new replies.

Advertisement