Hello,
I'm making a text based game to gain some experience in c++. I'm currently working on a kind of 'save' system.
I'm trying to read a file which contains the information for the save, it's contents are as follows:
1 2 3
|
# Save_001
level: 1
exp: 1
|
I'd first like to read the file, and output the content, with the exception of blank lines, and comments (#). After that I'll look into how to process the data in the file, but I'd first like the file to be read properly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
void saveGame::readFile() {
ifstream saveFile("save_001.txt"); // Open the file.
string line; // String to store content in.
if (saveFile.is_open()) { // Check wether the file is open.
while(saveFile.good()) { // Check for any error flags. (Could render the is_open call useless because this function only operates when its open?)
if(line.length() == 0) { // Conditional for a blank line. (Still have to think about the comments(#)).
cout << "line = 0"; // Should be something like: continue; or break; to skip this line. (???)
} else {
getline(saveFile, line);
cout << line << endl;
}
}
}
}
|
My expectations were that if I'd run this function, it would output each line, and give a message when encountered a blank line, but it seems to start atop of the file each time it encounters a blank line :/. It only gave a endless loop of "cout << "line =0";". How do I get it to go to the next line, instead of starting over?
Thanks
Edit: Got it, I checked for the length of the line before I got the line, thus a check for a empty line ( == 0 ). Only thing I'm wondering about is why saveFile.good never encounterd an eof :o. But thats nothing important really :o.