istream variable assign

I probably sound like a noob, and, infact, I am. Please forgive me if this is an easily found answer. I tried for an hour so far. I just don't know what to "search" for.

Basically what i'm trying to do is create a SIMPLE program. (game like)

But what my issue is... I want to make it save-able.
So I learned how to call in and write to files such as txt documents.
So I'm thinking ok, lets store all the variables and info into a text file, and also load things from a text file.

Okay here's where I am stuck.

I can have the program READ from the file.

In my example file "test.txt"
The only line on there is; "X=6"

I can have the program read that string and then cout << that string. I can also declare an integer variable called "X".

Problem: All I can do is have it read the string "X=6" and output the string. I have no idea how to either:

(Read only the constant. "6" and convert that string to an integer.),

(Read "X=6". Ignore the X=. Convert the maining part of the string, "6" to an integer variable.),

(Read "X=6" and conver THAT straight to C++ code. Basically copy the string from the text file and have it pasted into my Source Code somehow.),

I'm not sure which of these questions I need answered. Any/all answers would be appreciated.

But another thing i'd like to ask is::

(IF I had multiple lines on that txt file such as

x=6
y=7
z=8
a=4

How could I have it ONLY give me the information for X?

and from a more complicated (i think) scenario, How could I have it only give me the info for Z?


Lastly, How could I SEARCH for a particular line in a text file? For instance search for the 4th line (being A=4 in the example above). What I mean by SEARCH, is to find that line and manipulate/read/translate it.)

Thank you for any help.
Btw I'm using fstream for this.
Have you considered, instead of writing "<variable>=<value>" to the file, just writing "<value>"?

Read "X=6" and conver THAT straight to C++ code. Basically copy the string from the text file and have it pasted into my Source Code somehow.
This is essentially impossible. There are several ways to make the program behave as though this was happening, but I think they may be over your head, at least for now.

Lastly, How could I SEARCH for a particular line in a text file?
There are several ways, but they are all a variation of "count n newline sequences from the BOF". std::getline() returns a whole line, so a simple, though relatively inefficient, way to do it in C++ is to call std::getline() n times. Unless the file is formatted in a special way, there's no way to randomly jump to any line at no cost.
Well... what i'm trying to create is a simple Lemonade selling game.

Basically at BARE basics. I just want the person to be able to keep track of things after closing. Things such as "lvl", "Cash", number of cups, number of lemons. etc.

How could I store that on a txt file as just writing the numbers?
How would it know what to do with the numbers?

Is there a way to have it read only one full number at a time? And assign that as a variable?

Then continue down to the next number assigning it a different variable?

Cause if so thats prolly what im trying to do right now. I just want to be able to allow the player to pick up where they left off after closing.

And when its in a text file, what constitutes a single line?
Here is a sample code that may help you answer your questions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	fstream fl("lemonade.txt", fstream::out);

	int lvl = 11, lvl2;
	int lemons = 200, lemons2;
	double dblcash = 540.23, dblcash2;
	int cups = 400, cups2;

	fl << lvl << " " << lemons << " " << dblcash << " " << cups;

	fl.close();
	fl.open("lemonade.txt", fstream::in);

	fl >> lvl2 >> lemons2 >> dblcash2 >> cups2;

	cout << lvl2 << " " << lemons2 << " " << dblcash2 << " " << cups2 << endl;
}
So it reads one at a time?

My txt file would be something like,

10 230 322.45 25

??

And then when I called it to input

it puts the value up until the SPACE into lvl2

Then the value from that SPACE to the next spcae as lemons2 ?


And in theory if i wanted to create multiple save files. I could cout << "Which Save file would you like to use?" ;

And then have 3 different txt files

save1.txt, save2.txt, and save3.txt


then cin >> VARIABLESAVESTRING;
(declared this as a string already)

would the following do the job i'm wanting?

--
fstream xx1 (VARIABLESAVESTRING << ".txt", fstream::out);
--

If say i wanted save file 2

I input save2 when it asks.
Will this pull the information off of save2.txt?
** correction I think what i meant was would it save information to save2.txt (since i did fstream::out at this point)
If your requirement is to save to multiple files based on the file name input by the user, you would just create a new fstream/ofstream object and initialize with the input file name. Now, you may use this fstream/ofstream object to write data members to the file. Just note, you may also save multiple entries to the same file if that is option for you. I would suggest you to read the file reading & writing tutorial in this site as it covers all the basics.
Topic archived. No new replies allowed.