0

我正在尝试使用类结构和 getline() 函数将文本文件数据显示到列中,以读取文本文件并将数据转储到向量类中。但似乎该程序甚至没有运行到我的“while”循环然后退出程序。文本文件不为空。

下面是我的代码:

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();

}

编辑:文本文件示例
Joel:3:1:1
Mary:5:2:2
John:25:3:1

4

2 回答 2

2

第一次查找后,您的文件指针位于文件末尾。您需要将其重新定位到文件的开头。

if(size == 0)
{
    cout << "No results yet. Please TRY to win a game. AT LEAST~" << endl;
}
else
{
    readScoreChart.seekp(0,ios::begin);
    // all you other stuff
}
于 2013-07-17T06:58:43.013 回答
1

您需要回到文件的开头才能读取。更好的是,只是不要一开始就追求终点。

我会稍微重新构造代码——写一个operator>>从文件中读取记录,operator<<写一个记录到文件中。

class person {
    std::string name;
    std::string winning_time;
    std::string num_attempts;
    std::string level;

 public:
    bool operator<(person const &other) const { 
        return std::stoi(winning_time) < std::stoi(other.winning_time);
    }

    friend std::istream &operator>>(std::istream &in, person &p) { 
            std::string buffer;
            std::getline(in, buffer);
            std::istringstream is(buffer);

            std::getline(is,p.name,':');
            std::getline(is,p.winning_time,':');
            std::getline(is,p.num_attempts,':');
            std::getline(is,p.level);
            return in;
    }

    friend std::ostream &operator<<(std::ostream &os, person const &p) { 
             return os << std::setw(25) << p.name
                       << std::setw(22) << p.winning_time
                       << std::setw(17) << p.num_attempts
                       << std::setw(16) << p.level;
    }

};

有了这些,剩下的就变得相当简单了:

void show_header(std::ostream &os) { 
    cout << left
        << setw(20) << "Player Name "
        << setw(20) << "Winning Time "
        << setw(20) << "No. Of Attempts "
        << setw(20) << "Game Level " << "\n";
    std::cout << std::string(70, '-');
}

 void game::displayScoreChart(){ 
    std::ifstream in("ScoreChart.txt");

    // read the data:
    std::vector<person> people{std::istream_iterator<person>(in),
                               std::istream_iterator<person>()};

   if (people.empty()) {
       std::cout << "No scores yet."
       return;
   }

   // sort it by winning time:
   std::sort(people.begin(), people.end());

   show_header(std::cout);

   // display it:
   for (auto const &p : people) 
       std::cout << p << "\n";
   return 0;
}

如果你没有 C++11 编译器,作为一个简单的替换,stoi可以这样写:

int stoi(std::string in) { 
     return strtol(in.c_str(), NULL, 10);
}
于 2013-07-17T07:06:10.920 回答