【问题标题】:C++ Read numbers from a file with charactersC ++从带有字符的文件中读取数字
【发布时间】:2021-04-26 13:53:15
【问题描述】:

我希望能够将输入文件中的所有数字相加以求总和,但我不确定如何真正做到这一点?我可以使用一些指针。 这是输入文本文件包含的内容:

Mark Cyprus 21
Elizabeth Monroe 45
Tom McLaugh 82
Laura Fairs 3
Paul Dantas 102

这是我目前的代码

#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <stdlib.h>
#include <sstream>
using namespace std;

class Age
{
    //Create the variables
    string first_name, last_name;
    int age, sum_age, average_age;

public:
    bool isblank(const std::string& s)            
    {    //True if s is empty or only contains space and/or TABs.          
         return s.find_first_not_of(" \t")==std::string::npos;            
    }
    void readFile ()            
    {   
          ifstream file;           
          file.open("input_age.txt");     
          string line;                
          // file opened?                
          if (! file) {                    
              cerr << "can’t open input file." << endl; 
              exit(EXIT_FAILURE);                
           }
          // getline is true for as long as end of file is not reached            
          while(std::getline (file,line)){                    
              if (! isblank(line)){                        
                   // now the variable string "line" contains the line content  
                   file >> first_name;
                   file >> last_name;
                   file >> age;              
              }                
          }                
          file.close();            
          }    

有没有办法只捕捉年龄而不是阅读名字?我不相信我真的需要这个名字。所以最好删除那些 first_name 和 last_name 变量,但我不确定如何只读取文件中的数字。

【问题讨论】:

  • 当你的格式已知时,使用fscanf ...这是最简单的方法
  • 谢谢,我知道如何将年龄相加并求和。有没有办法创建一个打印出每个年龄的方法?当我尝试时,它只打印出最后一个年龄 102。 fscanf 对此有帮助吗?

标签: c++ file input numbers ifstream


【解决方案1】:

有问题:

while(std::getline (file,line)){                    
    if (! isblank(line)){                        
    // now the variable string "line" contains the line content  
    file >> first_name;
    file >> last_name;
    file >> age;              
}             

完成 getline 后,您已经在变量 line 中获得了 3 个值,但您从不使用它。您正在第二次阅读...所以您忽略了输入中的一半。

你应该做的是:

while(file >> first_name >> last_name >> age)
{
    //do whatever you want to do with age in here
}

如果由于到达文件末尾或输入格式错误而无法读取序列 2 个字符串和 1 个数字,则此循环将结束。

通过这样的阅读,您不需要isblank 方法。

【讨论】:

  • 非常感谢@vmp 我想出了如何将年龄相加并找到总和。有没有办法创建一个打印出每个年龄的方法?当我尝试时,它只会打印出最后一个年龄,即 102。
  • 您可以在 while 循环中使用 cout &lt;&lt; age &lt;&lt; '\n'; 打印,或者您可以声明一个向量来存储它们并在阅读完后立即打印:vector&lt;int&gt; v; while(...){ v.push_back(age);} for (int i : v) {cout &lt;&lt; i &lt;&lt; '\n';}
【解决方案2】:

这是一个似乎也同样有效的 C 解决方案:

#include <stdio.h>
#include <assert.h>
#define MAX_NAME_LENGTH 128
int main(int argc, char **argv)
{
    int number;
    char first_name[MAX_NAME_LENGTH];
    char second_name[MAX_NAME_LENGTH];
    FILE *fl=fopen("input.txt","rt");
    assert(fl && "could not open input file");

    while (fscanf(
        fl,
        "%s %s %d",
        first_name,
        second_name,
        &number) == 3)
    {
        printf("%s +++ %s +++ %d\n",first_name,second_name,number);
    }

    fclose(fl);
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    相关资源
    最近更新 更多