【问题标题】:c++ Leaderboard output from external txt file not working来自外部txt文件的c ++排行榜输出不起作用
【发布时间】:2017-04-21 02:54:29
【问题描述】:

您好,我一直在尝试做一个程序,编译器从 txt 文件中获取分数并按升序输出它们,但它对什么问题没有任何想法?我怎么能把它变成一个数组?

这是我正在使用的 infile.txt:

1 John Doe     23567
2 Larry Bird       21889
3 Michael Jordan   21889
4 James Bond       13890
5 Gary Smith       10987
6 GJ               9889
7 Vivien vien      8990
8 Samantha Carl    6778
9 Gary  Lewes      5667
10 Sybil Saban     4677

程序:

#include <iostream>
#include <fstream>
#include <string>
using std::ifstream;
using namespace std;


int main ()
{
    ifstream file_("infile.txt");
    int highscore;
    std::string name;
    int id;
    if (file_.is_open())
    {
    while(file_>>id >> name >> highscore)
    {

        std::cout<<id << " " <<name << " " <<highscore<<"";
    }
    file_.close();

    }


system ("pause");
return 0;

}

【问题讨论】:

    标签: c++ c++11 visual-c++


    【解决方案1】:

    当你使用

    file_>> id >> name >> highscore
    

    只读取名字,没有任何内容被读取到highscore,流进入错误状态,循环立即中断。

    你需要使用:

    std::string firstName;
    std::string lastNmae;
    
    file_ >> id >> firstName >> lastName >> highscore
    

    更新

    存在

    6 GJ               9889
    

    在文件中使得文件的简单读取变得困难。你必须使用完全不同的策略。

    1. 逐行读取文件。
    2. 标记每一行。
    3. 从第一个令牌中提取 ID。
    4. 从最后一个标记中提取高分。
    5. 将中间的标记组合成名称。

    这就是我的想法:

    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    #include <vector>
    
    using std::ifstream;
    using namespace std;
    
    int main ()
    {
       ifstream file_("infile.txt");
    
       int highscore;
       std::string name;
       int id;
       if (file_.is_open())
       {
          std::string line;
          while ( getline(file_, line) )
          {
             std::string token;
             std::istringstream str(line);
             std::vector<std::string> tokens;
             while ( str >> token )
             {
                tokens.push_back(token);
             }
    
             size_t numTokens = tokens.size();
             if ( numTokens < 2 )
             {
                // Problem
             }
             else
             {
                id = std::stoi(tokens[0]);
                highscore = std::stoi(tokens.back());
    
                name = tokens[1];
                for ( size_t i = 2; i < numTokens-1; ++i )
                {
                   name += " ";
                   name += tokens[i];
                }
    
                std::cout << id << " " << name << " " << highscore << std::endl;
             }
          }
       }
    }
    

    【讨论】:

    • 但我希望我的文本文件中的所有内容都能出现,而不仅仅是开始。
    • 我尝试使用您的策略,但我只是得到一个它已退出并出现代码 0 错误。
    • 你是说没有输出?在这种情况下,您的程序没有找到他的输入文件。
    • 如果是这种情况我很困惑,因为我确信我已经成功链接了我的文件并且它被称为 infile.txt。
    【解决方案2】:

    在一个结构中收集所有这些数据点,并将您的输入解析为这些结构的数组。

    在解析输入时,需要注意: 1) >> 如何实际工作 2) 指定的输入文件的名称为 1 和 2 个单词 3) 字符串可以访问 += 运算符,这可能很方便。

    完成此操作后,您将要按其分数成员对该结构数组进行排序,然后按顺序输出该数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      • 2013-03-30
      • 2017-02-21
      • 2013-01-25
      • 1970-01-01
      相关资源
      最近更新 更多