【问题标题】:Read a text file,C++读取文本文件,C++
【发布时间】:2018-06-24 05:13:31
【问题描述】:

我想阅读这个有 6 列的文本文件。问题是在第二列中,输入是类间隔的形式,我不知道如何阅读,我最后一次尝试没有做这项工作。此外,我想打印第二列和第三列,但它正在做其他事情。请帮忙。

#include <iostream>
#include <cmath>
#include <fstream>
#include <sstream>
int main()
{

    std::ifstream f("t2kflux.txt");
    std::string line;
    while (std::getline(f, line))
    {
        float serial;
        double Energy, Energy2;
        double mu;
        double mubar;
        double e;
        double ebar;
        std::istringstream ss(line);
        ss >> serial >> Energy >> Energy2 >> mu >> mubar >> e >> ebar;
        std::cout << Energy << "\t" << mu << "\n";
    }
}

数据样本是

                        Flux (/cm^2/1E21p.o.t/50MeV)
Bin#    Energy (GeV)    numu            anti-numu       nue             anti-nue
---------------------------------------------------------------------------------
1        0.00- 0.05     1.27e+04        1.64e+04        1.80e+02        2.97e+01
2        0.05- 0.10     5.40e+04        5.41e+04        1.02e+03        1.23e+02
3        0.10- 0.15     1.11e+05        3.98e+04        2.17e+03        2.12e+02
4        0.15- 0.20     1.75e+05        3.10e+04        3.23e+03        2.86e+02
5        0.20- 0.25     2.62e+05        2.78e+04        4.08e+03        3.56e+02
6        0.25- 0.30     3.68e+05        2.77e+04        4.73e+03        4.15e+02
7        0.30- 0.35     4.83e+05        2.83e+04        5.27e+03        4.61e+02
8        0.35- 0.40     5.97e+05        2.92e+04        5.58e+03        4.93e+02
9        0.40- 0.45     7.21e+05        2.97e+04        5.80e+03        5.18e+02
10       0.45- 0.50     8.89e+05        2.93e+04        5.77e+03        5.31e+02
11       0.50- 0.55     1.11e+06        2.93e+04        5.72e+03        5.35e+02
12       0.55- 0.60     1.24e+06        2.98e+04        5.53e+03        5.31e+02
13       0.60- 0.65     1.23e+06        2.92e+04        5.33e+03        5.26e+02
14       0.65- 0.70     1.14e+06        2.80e+04        5.03e+03        5.15e+02
15       0.70- 0.75     9.10e+05        2.59e+04        4.76e+03        5.05e+02

【问题讨论】:

  • 使用好旧的sscanf() 可能更容易。或者,将Energy 更改为std::string,然后删除最后一个字符(-)。
  • 请您详细说明一下?
  • 在浮点数之后有一个“-”是一种奇怪的数字符号。它是无条件地一直存在吗?会不会是“+”?会不会是“”?
  • 您需要跳过前三行,因为它们不是预期的格式。

标签: c++ file-handling


【解决方案1】:

根据我对您正在尝试做的事情的了解,我认为您有两个问题。

首先是您正在阅读前三行,就好像它们是数据一样,但它们不是,它们是标题信息。所以我认为你需要跳过前三行:

// Skip first three lines
std::getline(f, line);
std::getline(f, line);
std::getline(f, line);

另一个是你需要在你的类间隔值之间读取破折号'-'字符。

总之是这样的:

#include <iostream>
#include <cmath>
#include <fstream>
#include <sstream>

int main()
{

    std::ifstream f("t2kflux.txt");
    std::string line;

    // Skip first three lines
    std::getline(f, line);
    std::getline(f, line);
    std::getline(f, line);

    while(std::getline(f, line))
    {
        float serial;
        double Energy, Energy2;
        double mu;
        double mubar;
        double e;
        double ebar;

        char dash; // to absorb the '-' separator

        std::istringstream ss(line);

        ss >> serial >> Energy >> dash >> Energy2 >> mu >> mubar >> e >> ebar;

        std::cout << Energy << "\t" << mu << "\n";
    }
}

【讨论】:

  • 非常感谢!它完全符合我的需要。我不知道如何包含破折号!
  • @MasoomSingh dash 是我添加的变量的名称。它只是一个char,因此您可以将分隔符读入其中。
【解决方案2】:

我已经编写了以下代码,它对我来说工作正常。希望对您有所帮助。

#include<iostream>
#include<cmath>
#include<fstream>
#include<string>
#include<sstream>
 int main()
{
std::ifstream f("honey.txt");
std::string line;
while(std::getline(f,line))
{
float serial;
std::string Energy,Energy2;
std::string mu;
std::string mubar;
std::string e;
std::string ebar;
std::istringstream ss(line);
ss>>serial>>Energy>>Energy2>>mu>>mubar>>e>>ebar;
std::cout<<Energy<<"\t"<<mu<<"\n";
}
}

解决此问题的其他方法是:

第 1 步:首先创建一个程序,从文件中删除破折号(-)。

第 2 步:使用您的程序(如问题),它将正常工作。

【讨论】:

    猜你喜欢
    • 2021-03-10
    • 2015-05-16
    • 1970-01-01
    • 2014-09-03
    • 2017-05-02
    • 1970-01-01
    • 2011-06-29
    相关资源
    最近更新 更多