从文件到换行符读取空格分隔的数字 [英] read space delimited number from file up to newline character

查看:188
本文介绍了从文件到换行符读取空格分隔的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下数据的文本文件。

I have a text file that contains the following data.

第一行是:

5 4 3 2 1

第二行是:

1 2 3 4 5

我试图一次读取一行数据,因为我的第一个链表对象将使用第一行的数据,我的第二个链表对象将使用第二行的数据。我能想到的最好的是以下函数:

I am trying to read data from one line at a time because my first linked-list object is going to use the data from the first line and my second linked-list object is going to use data from the second line. The best I have been able to come up with is the following function:

void polynomial::allocate_poly(std::ifstream& in, const char* file, const char* number)
{

    in.open(file);

    std::string str;
    char b;
    int m = 0;

    for(int i = 0; !in.eof(); ++i)
    {
        in >> b;
        m = b - '0';
        a.insert(m);
    }

此方法存在一些问题。我在for循环中尝试了不同的二元运算符,例如 b =='\ n',当b是换行符时,它们似乎都没有触发。

There is a few problems with this approach. I have tried different binary operators in my for loop such as b == '\n' and none of them seem to trigger when b is a newline character.

同样以这种方式分配文件中的数字看起来像
5 5 4 3 2 1 1 2 3 4 5 ,所以它似乎在某个地方复制了额外的5,我不确定这是否是eof位。

Also allocating the numbers from the file this way it looks like 5 5 4 3 2 1 1 2 3 4 5 , so it seems to be copying an extra 5 somewhere, I am not sure if this is the eof bit or not.

我也尝试过使用getline函数但由于某种原因它似乎只复制第一个整数然后转储文件的其余部分。我当然知道我没有正确使用它,但我找到的所有示例都是输入文件名,例如 cin.getline ,我希望能够传递我的文件改为在运行程序时将其命名为命令行参数。

I have also attempted to use the getline function but for some reason it seems to only copy the first integer and then dumps the rest of the file. I know certainly I am not using it correctly but all the examples I can find are for typing the file name such as cin.getline and I want to be able to pass my file name as a command line argument when running the program instead.

我的问题是如何在第一行分配数字直到换行符char然后传递 ifstream在变量到另一个对象分配第二行?谢谢你的帮助。

My question is how can I allocate the numbers on the first row up to the newline char and then pass the ifstream in variable to another object to allocate the second line? Thanks for your help.

推荐答案

你没说什么 a 是的,但没关系:如果你想要基于行的解析,你需要在外部循环中有 getline 。此外,从不使用 eof ,因为它没有按照您的意愿行事。而是使用隐式转换到 bool 来检查操作是否成功。

You don't say what a is, but never mind: If you want line based parsing, you need to have getline in the outer loop. Also, never use eof, as it doesn't do what you want. Rather use the implicit conversion to bool to check if an operation succeeded.

这是典型的演出:

#include <sstream>
#include <fstream>
#include <string>

std::string line;
std::ifstream infile("myfile.txt");

while (std::getline(infile, line))  // this does the checking!
{
  std::istringstream iss(line);
  char c;

  while (iss >> c)
  {
    int value = c - '0';
    // process value
  }
}

然而,这个从 char 转换为 int 既麻烦又脆弱。为什么不直接读取整数?

However, this conversion from char to int is cumbersome and fragile. Why not read an integer directly?

  int value;
  while (iss >> value) { /* ... */ }






编辑:根据您的评论,这里是准确读取两行的代码:


Based on your comment, here's the code to read exactly two lines:

std::string line;
int val;

if (std::getline(infile, line))
{
  std::istringstream iss(line);
  while (iss >> value) { /* process first line */ }
}
if (std::getline(infile, line))
{
  std::istringstream iss(line);
  while (iss >> value) { /* process second line */ }
}

这篇关于从文件到换行符读取空格分隔的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆