1

I am trying to make a save state file and need to break it into words. I am unable to find any where that gives an explanation on how to do this, I am only able to break it into lines via the getline() command.

if (saveStateRead.is_open())
    {
        for (int i = 0; i < 14; i++)
        {

            while (getline(saveStateRead, saveState[i]))
            {
                cout << saveState[i];
            }
        }
        saveStateRead.close();
    }

This is the code I have that is writing it to my array. I would like it to break into individual digits by both space and just by the next digit. My save state file looks like this

00 00 00 00 00 00 00 00 00 00 00 00

if you have any suggestions they would be greatly appreciated.

Keaton H
  • 11
  • 5
  • There are a few ways to do this. I would suggest trying something before asking a question on SO. You could try re-reading your saveState[i] into a stringstream and from there do a getline on it, this time using a space delimiter instead of newline delimiter which getline(...) uses by default. Style-wise I'd suggest not using namespace std to avoid namespace collision and instead opt for std::, also avoid magic numbers '14' in this case comes from somewhere, define it so it is easier to change in future. Also the logic of the while loop inside the for loop seems off. – rossb83 Oct 16 '18 at 20:16
  • I believe you might get somewhere using `strtok` although it does trash the string, so you need to make a copy before running it. – Tim Randall Oct 16 '18 at 20:18
  • Answer 1, option 2 in [Read file line by line using ifstream in C++](https://stackoverflow.com/questions/7868936/read-file-line-by-line-using-ifstream-in-c) – user4581301 Oct 16 '18 at 20:57

0 Answers0