Я пытаюсь отобразить данные текстового файла в столбцы, используя структуру класса и функцию getline() для чтения текстового файла и дампа данных в векторный класс. Но похоже, что программа даже не запустилась в мой цикл while, а затем покинула программу. Текстовый файл не пуст.getline не работает как ожидалось
Ниже мой код:
void ScrambleWordGame::displayScoreChart() {
//open file
fstream readScoreChart("ScoreChart.txt");
string line = "";
//vector to store data in
vector<personResult> perResult;
personResult person;
//while file is open, do stuff
if(readScoreChart.is_open())
{
//check through the file
readScoreChart.seekp(0,ios::end);
//get the size of the file's data
size_t size = readScoreChart.tellg();
if(size == 0)
cout << "No results yet. Please TRY to win a game. AT LEAST~" << endl;
else
{
//create the 1st row with 4 column names
cout << left
<< setw(20) << "Player Name "
<< setw(20) << "Winning Time "
<< setw(20) << "No. Of Attempts "
<< setw(20) << "Game Level " << endl;
//fill the second line with dashes(create underline)
cout << setw(70) << setfill('-') << "-" << endl;
//read the file line by line
//push the 1st line data into 'line'
cout << getline(readScoreChart,line);
while(getline(readScoreChart,line))
{
//create stringstream n push in the entire line in
stringstream lineStream(line);
//reads the stringstream and dump the data seperated by delimiter
getline(lineStream,person.playerName,':');
getline(lineStream,person.winningTime,':');
getline(lineStream,person.noOfAttempts,':');
getline(lineStream,person.gameLvl);
//sort the results based on their timing
//sort(perResult.begin(),perResult.end(),pRes);
//display the results
cout << left
<< setfill(' ')
<< setw(25) << person.playerName
<< setw(22) << person.winningTime
<< setw(17) << person.noOfAttempts
<< setw(16) << person.gameLvl
<< endl;
}
}
}
readScoreChart.close();
}
Edit: Пример Textfile
Иоиля: 3: 1: 1
Марии: 5: 2: 2
John: 25 : 3: 1
Не могли бы вы разместить пару строк текстового файла, который вы пытаетесь разобрать, пожалуйста? – jrd1
'readScoreChart.seekp (0, ios :: end);' - файл может быть пустым, но вы пропускаете все содержимое. – Casey
@Casey Я делаю это, чтобы проверить, пустой ли текстовый файл или нет. После проверки указатель должен вернуться наверх, не так ли? –