Sign in to follow this  

Read/store a single line from a text file.

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

I am trying to save data to a file and then read it back and store it in a variable.

So what I have now is

void PauseMenu::saveGame()
{
std::ofstream outfile;

outfile.open("SaveGame.ini", std::ios::out);

outfile << "Ship XPOS " << std::endl << game->player.ship.trueX << std::endl;
outfile << "Ship YPOS " << std::endl << game->player.ship.trueY << std::endl;
outfile << "Ship Rotation " << std::endl << game->player.ship.rotation << std::endl;
}

This is the output.

Ship XPOS
960
Ship YPOS
600
Ship Rotation
0

That works great. Then I could read back line 1(starting at 0?) and store it back as game->player.ship.trueX when I went to load the file.
Only problem is, I don't know how to do that. I found a possible solution googling, but the tutorial was difficult for me to understand.

This is my current Loading code which I know doesn't work. That is what I tried to write according to the tutorial. Those commands are probably totally wrong. I am very unfamiliar with seekg and other commands similar to it.


void PauseMenu::loadGame()
{
std::ifstream infile;

infile.open("SaveGame.ini", std::ios::in);

/*char letter;

while(infile.get(letter))
std::cout << letter;*/

infile.seekg (2, std::ios::end);
game->player.ship.trueX = infile.tellg();
//std::cout << game->player.ship.trueX;

infile.seekg (4, std::ios::end);
game->player.ship.trueX = infile.tellg();
//std::cout << game->player.ship.trueY;

infile.seekg (6, std::ios::end);
game->player.ship.trueX = infile.tellg();
//std::cout << game->player.ship.rotation;

}


Thanks. I really appreciate your help/time in reading this. I can also give any other information you might need.

Share this post


Link to post
Share on other sites
you could go about this differently, for example using std::string to store the entire line, than parsing through the string for certain elements you need.


infile.open("file.ext");
std::string line;

while(!infile.eof())
{
getline(infile, line);

/*
than you can parse through the string positions
example would be creating a token character and storing it there
than checking if that token equals certain elements and do what
you want with it.
*/

}




EDIT: Also since it looks like you are just storing integers, you could get rid of the titles in the save file as there not needed, since you know what they do and just do something like,

getline(infile, line);
posX = atoi(line.c_str());
//than on next line read same thing for posY

Share this post


Link to post
Share on other sites
The problem with:

getline(infile, line);
posX = atoi(line.c_str())

is that line is now equivalent to 9606000.
This is the three lines
960
600
0
All combined together. How do I read each line separately?
Sorry if i'm missing a simple solution to this.
Thanks for your help so far.

Share this post


Link to post
Share on other sites
I dont know why yours is adding up in one line, 960 600 and 0 should overwrite the existing line previously so your output to the console should read

960
600
0

as it is read in...

here is the example I was using that gave me this output,


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
ifstream infile;
infile.open("save.txt");

string line;

if(infile.is_open())
{
while(!infile.eof())
{
getline(infile, line);
cout << line << endl;
}

infile.close();
}
return 0;
}

Share this post


Link to post
Share on other sites
Ok this should work for you, I just tested it,


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
ifstream infile;
infile.open("save.txt");

std::string line;

int x = 0;
int y = 0;
int a = 0;

int count = 1;

if(infile.is_open())
{
while(!infile.eof())
{
getline(infile, line);
if(count == 1)
x = atoi(line.c_str());
else if(count == 2)
y = atoi(line.c_str());
else if(count == 3)
a = atoi(line.c_str());

count++;
}

infile.close();
}
else
cout << "Unable to open file..." << endl;

return 0;
}


Share this post


Link to post
Share on other sites
The following code does almost the same thing. It will work on any whitespace separator, not just newlines. It will also correctly detect data conversion errors, whereas the original would silently set your variables to 0.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
// You can open a file stream in the same line you declare it.
ifstream file("save.txt");

if(fille.is_open())
{
// Scope your variables as close to use as possible
int x = 0;
int y = 0;
int a = 0;

// Use the read operation itself as the condition
if(file >> x >> y >> a)
{
// Values loaded correctly!
cout << "x: " << x << " y: " << y << " a: " << a << '\n';
}
else
{
// Number conversion error, or end-of-file reached too early
// You can test with file.eof() to determine the exact error, like so:
cerr << "Error loading values: ";
if(file.eof())
{
cerr << "Not enough numbers in file";
}
else
{
cerr << "Non-numeric data found in file";
}
}
}
else
{
// It is good practise to always use braces
// Otherwise if you accidentally add more code to a block
// without them, you'll have a strange logic bug that can be hard
// To spot
cerr << "Unable to open file..." << endl;
}

// std::fstream objects close themselves when they fall out of scope
// We don't need an explicit call to close()
return 0;
}


Try this version out against the previous with a wide variety of files, like empty files, files with garbage data in them, etc. You should see that

Share this post


Link to post
Share on other sites

This topic is 2661 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this