1

I've been trying to retrieve saved data from a text file. The data stored are both numbers, separated by a ~. I've managed to get it to print out one of the lines (the top line) however I've been unable to figure out how to proceed through the entire file.

There are only two numbers (integers) on each line, an X and Y position of another vector. The idea is to assign each integer to the respective variable in the vectors. I've not managed to get that far since I can't get it to go past line 1. But I'd thought that by having an array size of 2, and the array temporarily stores the value, assigns it to the vector, then overwrites it with the next value(s) that could work. But again not managed to get that far.

Below is the code I've been trying to use;

........
string loadZombieData;
loadFile >> loadZombieData; //Data gets read from the file and placed in the string

vector<string>   result; //Stores result of each split value as a string


stringstream  data(loadZombieData);
string line;
while(getline(data,line,'~'))
{
    result.push_back(line); 
}

for(int i = 0; i < result.size(); i++){
    cout << result[i] << "  ";
}
.......

Just to clarify, this is not my code, this is some code I found on Stackoverflow, so I'm not entirely certain how it all works yet. As I said, I've been trying to get it to read multiple lines, then using the for loop was going to assign the results to the other vector variables as needed. Any help is appreciated :)

NeoKuro
  • 49
  • 1
  • 7
  • Still not very clear what you wanted in spite of reading 3 times... Can you give an example of the file you loaded and the expected output? – A Lan Mar 24 '14 at 02:15
  • Sorry. The file is something like this: `10~8\n4~13\n8~2` etc (the number of pairs varies depending on how many zombie (one of the other vectors) were saved. The program currently saves the X and Y positions (x~y) and what I'm trying to do is retrieve the values and apply these values to the x and y positions of the vectors (basically reload a previous position). If that helps? :/ – NeoKuro Mar 24 '14 at 03:06

1 Answers1

0

Use two while loops:

std::vector<std::string> result;
std::vector<int> numbers;

std::string filename;
std::ifstream ifile(filename.c_str());

if (!ifile.is_open()) {
    std::cerr << "Input file not opened! Something went wrong!" << std::endl;
    exit(0);
}

std::string temp;

//loop over the file using newlines as your delimiter
while (std::getline(ifile, temp, '\n')) {
   //now temp has the information of each line.
   //create a stringstream initialized with this information:

   std::istringstream iss(temp);//this contains the information of ONE line

   //now loop over the string stream object as you would have in your code sample:
   while(getline(iss, temp,'~'))
   {
       //at this point temp is the value of a token, but it is a string
       result.push_back(temp); //note: this only stores the TOKENS as strings

       //so to store the token as a int or float, you need to convert it to that
       //via another stringstream:

       std::istringstream ss(temp);

       //if your number type is float, change it here as well as in the vector
       //initialization of `numbers`:
       int num = 0;
       //this checks the stream to ensure that conversion occurred.
       //if it did, store the number, otherwise, handle the error (quit - but, this is up to you)
       //if stringstreams aren't your cup of tea, try some others (refer to this link):
       //http://stackoverflow.com/questions/21807658/check-if-the-input-is-a-number-or-string-c/21807705#21807705
       if (!(ss >> num).fail()) {
           numbers.push_back(num);
       }
       else {
           std::cerr << "There was a problem converting the string to an integer!" << std::endl;
       }
    }
}

Note: this version stores the numbers verbatim: i.e. without a sense of how many numbers were on a line. However, that is reconcilable as all you have to do is output n numbers per line. In your case, you know every 2 numbers will be represent the numbers in a line.

This requires:

#include <string>
#include <vector>
#include <cstdlib>
#include <sstream>
jrd1
  • 10,358
  • 4
  • 34
  • 51
  • Hi thanks for the help. This is probably something silly, but I can't seem to get the 'ifile' to open. It just reverts to the error then closes. I've tried defining the `filename` string as well the filename, leaving as is, defining it with the fstream and the `loadZombieData` variables, and they both don't work either. – NeoKuro Mar 24 '14 at 03:03
  • @NeoKuro: Did you put the name of the file in `filename`? – jrd1 Mar 24 '14 at 03:25
  • I tried that I think lol. I tried `filename = "saveZombieData.txt"` I tried `filename = loadZombieData` as well as `filename = loadFile`. None of them worked, they all spat out some error (IE the programmed one to make the program close if ifile isn't open) – NeoKuro Mar 24 '14 at 03:37
  • @NeoKuro: Hmmm. Where is your data file? It should be in the same directory as the compiled file if your filename is: "saveZombieData.txt" – jrd1 Mar 24 '14 at 03:55
  • I feel silly now. I just realized the file is actually called saveZombieFile.txt not data haha. EDITED: I fixed the other issue, minor placement issue of the for loop. Seems to be working now, thanks everyone. Now just gotta figure out how to assign the number left of the `~` to x and the numebr right of it to y :) – NeoKuro Mar 24 '14 at 04:03
  • @NeoKuro: Use another stringstream for that. Do you want me to address that for you as well? Also, if you found my answer useful, please consider it for acceptance. :) – jrd1 Mar 24 '14 at 04:07
  • yes please if you don't mind haha. And certainly. Was gonna do that in a sec just before you mentioned it haha :) – NeoKuro Mar 24 '14 at 04:24
  • Thanks a lot. I'm still having trouble, butat least it shows a number. For some reason when I do `cout << " " << numbers[i];` (within a for loop ofc) it always gives the number 1. Likewise if I ask for it's size, and I'm not sure why it does this? I'll have a look again tomorrow, before I come back as its quite late here now (5am lol) – NeoKuro Mar 24 '14 at 04:53
  • @NeoKuro: That shouldn't happen. My bad! That was an error on my mistake! I've fixed it. Apologies about that! – jrd1 Mar 24 '14 at 05:01
  • haha no problem. everyone makes small mistakes (some *cough* me *cough* more than others lol) Thanks again :) – NeoKuro Mar 24 '14 at 05:05