# Quick .dat question

## Recommended Posts

Howdy, OK, so I encountered a small problem with .dat files. I am doing this:

ofstream file;

int noob=50;

file.open("thingy.dat");
file << noob << endl;


Just to make teh file (test program) But now I want to access the variable "noob" from .dat file "file" in a different program. How do I do that?

##### Share on other sites
Quote:
 Original post by Acid rainBut now I want to access the variable "noob" from .dat file "file" in a different program. How do I do that?
In short, you do the opposite:

ifstream file;int noob;file.open("thingy.dat");file >> noob;

There's more to it of course (error checking, for one thing), but that's the essence of it.

##### Share on other sites
Does that only work for DOS apps? I want to grab the value and pass it to a function that Renders it to the screen. How do I do that?

##### Share on other sites
Quote:
 Does that only work for DOS apps?
No, it's just C++ - it has nothing to do with DOS (not directly at least).
Quote:
 I want to grab the value and pass it to a function that Renders it to the screen. How do I do that?
Depends on what you mean by 'render'. If you just want to display the value, you can print it to the console using a global stream object such as cout.

##### Share on other sites
By render I meen DRAW to the screen. I have a function ( from Allegro library ) that excepts an integer and blits it to the screen. How would I pass the variable "noob" from file "file" to such a function? I JUST need to grab the "noob" variable.

##### Share on other sites
Quote:
 Original post by Acid rainBy render I meen DRAW to the screen. I have a function ( from Allegro library ) that excepts an integer and blits it to the screen. How would I pass the variable "noob" from file "file" to such a function? I JUST need to grab the "noob" variable.
After extracting the value from the file stream as demonstrated in my previous example, just pass the noob variable as an argument to the Allegro function in question. (I'm not familiar with the Allegro API, so I can't be more specific than that.)

##### Share on other sites
What if I wanted to do something like this:

 ofstream file;int noob=50;file.open("thingy.dat");file << "NOOB: " << endl;file << noob << endl;

So that I could open the .dat file in windows NOTEPAD and edit the variables value. How would I then load the variable?

##### Share on other sites
Quote:
 Original post by Acid rainWhat if I wanted to do something like this:*** Source Snippet Removed *** So that I could open the .dat file in windows NOTEPAD and edit the variables value. How would I then load the variable?
Editing the text file is unrelated to reading and writing the file in C++ - you don't have to do anything special in your code to make this possible. If the file contains the text "NOOB: 1", you can open it in NotePad, change the '1' to a '2', save it, run the program that reads the file and prints the value to the screen, and the program will display the value '2'. It's as simple as that.

The only caveat here is that you've now complicated the file format somewhat by writing out the string "NOOB: " prior to the variable value, so you'll have to take this into account when reading the data back.

##### Share on other sites

NOOB: 50

I tried reading the value after I edited it, but I can't. When I try reading it with my program, the new number doesn't come up. I think it's trying to read the string "NOOB:" as the number. How do I make it load from AFTER the "NOOB:" string?

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628383
• Total Posts
2982371

• 10
• 9
• 15
• 24
• 11