Jump to content
  • Advertisement
Sign in to follow this  
Acid rain

Another .dat question

This topic is 4184 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

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
Advertisement
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

#include <string>
#include <fstream>

void f()
{
std::ifstream is("whatever.dat");
std::string s;
int noob;

is >> s >> noob;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by EasilyConfused

#include <string>
#include <fstream>

void f()
{
std::ifstream is("whatever.dat");
std::string s;
int noob;

is >> s >> noob;
}


Thanks!

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
Quote:
Original post by Nvii
also, should close the file when your done

file.close();


You don't need to do this as the destructor for fstream objects closes files automatically.

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
Sign in to follow this  

  • 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!