I am trying to read information from a file into string variables. Here is how the data appears in the file:
TFTFTFTFTFTFTFTFTFTF ABC12345 TTFFTTFF TTTTTTTTTTF
The first two spaces are delimiters between the strings I want. However, that third space must be INCLUDED in the third string. So by the end I'd like to have three strings:
str1 = "TFTFTFTFTFTFTFTFTFTF"
str2 = "ABC12345"
str3 = "TTFFTTFF TTTTTTTTTTF"
That space in the third string is giving me real problems. I just can't figure out a way to deal with it. Following is the code that has given me the most success:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
int main()
{
string answersString = "";
string studentAnswersString = "";
string idString = "";
char answerChar;
char idChar;
char studentAnswerChar;
ifstream trialRunsFile;
trialRunsFile.open("testScoresTrial.txt");
while (!trialRunsFile.eof())
{
for (int i = 0; i < 20; i++)
{
trialRunsFile >> answerChar;
answersString = answersString + answerChar;
}
trialRunsFile.ignore(1);
for (int j = 0; j < 8; j++)
{
trialRunsFile >> idChar;
idString = idString + idChar;
}
trialRunsFile.ignore(1);
for (int i = 0; i < 20; i++)
{
trialRunsFile >> studentAnswerChar;
studentAnswersString = studentAnswersString +
studentAnswerChar;
}
}
cout << "answersString is " << answersString << endl;
cout << "idString is " << idString << endl;
cout << "studentAnswersString is " << studentAnswersString << endl;
trialRunsFile.close();
}
|
Here is the output of this code:
answersString is TFTFTFTFTFTFTFTFTFTF
idString is ABC12345
studentAnswerString is TTFFTTFFTTTTTTTTTTFF
Now this is VERY CLOSE to what I want, but the third string is wrong in two ways: it seems to have ignored the space entirely, but instead of ignoring the space and only printing 19 of the 20 characters, it has gone ahead and appended an extra 'F' on the end for some reason! Obviously my code is telling the computer to do this, but I can't for the life of me figure out where that command happens.