Reading certain lines from a .txt into variables?

Ok, so I am pretty new to C++ and have recently started a basic console application using Code::Blocks.
Using this little snippet of code, I am able to save the string 'name' into the first line of save.txt:
1
2
3
4
5
  ofstream myfile;
  myfile.open ("save.txt");
  myfile << name;
  myfile << x;
  myfile.close();


I then use this code to load 'name' and save it as that string. It works fine, using this:
1
2
3
4
5
6
7
   int loadgame() {
                ifstream in;
                in.open("save.txt");
                in >> name;
                cout << "Loading "<< name <<"'s save file.";
                select();
                }

Now, how would I go about saving even more strings and integers to the same file? Is there such a way to save it to a different line, and then load it from that specific line? Or must I make a new .txt for each variable? I've looked in many places but can't find a clear way to do it.
Thanks.
Last edited on
You'd need to know what type of data you are reading...

You are loading game stuff so lets say you have a list of coordinates in this sort of a file:
X Y Z
2 7 4
6 4 1
3 9 6
2 8 5


We can read it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct point
{
    int x;
    int y;
    int z;
};

std::vector<point> points;
std::ifstream fin("save.txt");

{
    std::string first_line; 
    getline(fin, first_line); // waste the first line ("X Y Z")
}

while( fin.good() )
{
    point temp;
    fin >> temp.x >> temp.y >> temp.z;
    points.push_back(temp);
}


Now you have a vector of points! Add as many as you like.

If you have a list of names, it's the same sort of thing, but even simpler to write:
1
2
3
4
5
6
std::ifstream fin("names.txt");
std::vector<std::string> names;
std::string name;

while ( std::getline(fin, name) )
    names.push_back(name);


Now you can use each name like so:
1
2
for (std::vector<std::string>::iterator it = names.begin(); it!=names.end(); ++it)
    std::cout << *it;

or
1
2
for (int i = 0; i < names.size(); ++i)
    std::cout << names[i];
Last edited on
Well if I wasn't confused beforehand, I am now. Not too good with vectors. XD

Using the second box of code you supplied I was able to successfully read string name from the .txt, and it always read it from the last line. As for the 1st one, I'm not sure how that works.

Could you possibly create an example where string name is read separately from, say, integer e, in the save.txt file?

If it helps, the reason for me wanting to save these is this:
I want the user's input name to save, along with int e, which is the amount of coins. This way it'll act as game save data so that the user can load them every time they start the program.

I am very grateful for your help, good sir, and sorry for not being too C++ competent. :P
Ah, so you have a text file like this:
chris 5
tara 7
Sam 13
Joey 15


Let's first make a structure that will contain names and numbers:
1
2
3
4
5
struct sEntry
{
    std::string name;
    int number;
};


Since you aren't familiar with std::vector yet, let's make an array for each entry. Note that we have limited the number of entries to 256. If we wanted to make it variable, we'd have to use vectors or dynamic memory allocation.
sEntry entries[256];

Now, we want to read in our stuff:
1
2
3
4
5
6
7
8
std::ifstream fin("input.txt"); // opens the text file
int nb_entries; // Keeps track of the number of entries read.

for (nb_entries = 0; fin.good() && nb_entries < 256;  nb_entries++;) // Keep going until we hit the end of the file:
{
    fin >> entries[nb_entries].name;
    fin >> entries[nb_entries].number;
}


One limitation of this is that the operator>> will only extract up until the first white space. That means that if a name has a space in it, the whole thing will fail.
Last edited on
Okay, so to test this out I made a new program to simply read the four names and numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
string name;
int number;
struct sEntry
{
    std::string name;
    int number;
};
sEntry entries[256];
std::ifstream fin("input.txt"); // opens the text file
int nb_entries; // Keeps track of the number of entries read.

for (nb_entries = 0; fin.good() && nb_entries < 256;  nb_entries++) // Keep going until we hit the end of the file:
{
    fin >> entries[nb_entries].name;
    fin >> entries[nb_entries].number;
    cout <<"Here, "<< name <<" is name.\n";
    cout <<"Here, "<< number <<" is number.\n";
}
}


I added those cout lines to check to see if it was reading the whole file and how the name and number were being saved. It read all four lines, as the cout lines were repeated four times (chris, tara, sam, and joey, just like I had put into input.txt), but the output did confuse me a bit.

Here,  is name.
Here, 2686760 is number.
Here,  is name.
Here, 2686760 is number.
Here,  is name.
Here, 2686760 is number.
Here,  is name.
Here, 2686760 is number.

Process returned 0 (0x0)   execution time : 0.012 s
Press any key to continue.



Well, I suppose it's progress, right? XD
Hi, I am new here..

@squirrelboy1225
1
2
cout <<"Here, "<< (!)name <<" is name.\n";
    cout <<"Here, "<< (!)number <<" is number.\n";


You printed the lone variables name and number but not the array entries[]..
Ah. How does one print the array entries in this case? I've found many things online but can't seem to find anything to specifically 2 entries like this (in this case, name and number).
Thanks!
Change your for loop to this:

1
2
3
4
5
6
7
for (nb_entries = 0; fin.good() && nb_entries < 256;  nb_entries++) // Keep going until we hit the end of the file:
{
    fin >> entries[nb_entries].name;
    fin >> entries[nb_entries].number;
    cout <<"Here, "<< entries[nb_entries].name <<" is name.\n";
    cout <<"Here, "<< entries[nb_entries].number <<" is number.\n";
}
Oh, yeah, I got that, just forgot to post it here. It makes sense now :) Thanks for the help everyone!
Topic archived. No new replies allowed.