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:
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.
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:
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
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.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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.
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!
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";
}