Sign in to follow this  
Acid rain

Another .dat question

Recommended Posts

Howdy, OK, I am doing this to write to a .dat file:

ofstream file;
int noob=50;

file.open("thingy.dat");

file << "NOOB: " << endl;
file << noob << endl;




Then I open the .dat file with NOTEPAD and edit the "noob" variable to 100. How do I then read/extract the "noob" variable? I tried this but it doesn't work:

ifstream file;
int noob;

file.open("thingy.dat");

file >> noob;



That code doesn't read the "noob" variable. Which brings me to my main question. How do I read the "noob" variable if it comes after a string "NOOB: "? [Edited by - Acid rain on April 7, 2007 6:50:55 PM]

Share this post


Link to post
Share on other sites
You write two things to the file (the string "NOOB: " and then an integer) but you only try to extract one thing, the integer. You have to read in the text first to reach the integer value.

Share this post


Link to post
Share on other sites
int num;
std::string text;

file >> text >> num;


the >> operator will read in a string from the file, which is "NOOB:", and then the >> operator will read in an integer and store it in num.

EDIT: you asked for explanation then took it back! :P

Share this post


Link to post
Share on other sites
Btw, if the .dat thing is throwing you off.. you could name it .txt, .lol, .iamsocool, .wooooooo or anything you'd like. We'll, I'm sure there are some limits/exceptionms, but you get what I'm saying.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this