使用getline()从文件读取多行, [英] Reading multiple lines from a file using getline()

查看:312
本文介绍了使用getline()从文件读取多行,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读入,然后输出三行文本文件的内容,如下:

I am trying to read in and then output the contents of a text file with three lines, as follows:

Bob Dylan 10 9

Bob Dylan 10 9

John Lennon 8 7

John Lennon 8 7

David Bowie 6 5

David Bowie 6 5

,我只想输出行,例如firstName LastName number1 number2。

For each line, I just want to output the line, i.e. firstName LastName number1 number2.

我使用下面的代码:

int num1;
int num2;
string firstName;
string lastName;
string fullName; 
ifstream inFile;

inFile.open("inputFile.txt");

while (getline(inFile, firstName))
    {
        inFile >> firstName >> lastName >> num1 >> num2;

        fullName = firstName + " " + lastName;

        cout << fullName << " " << num1 << " " << num2 << endl;
    }

inFile.close();

这个输出有两个问题。首先,第一行不输出,虽然从实验我知道它读取它。第二,在最后2行被读入和输出(如所期望的)后,程序显示最后一行除了名字的一切(在这种情况下,最后打印的东西是Bowie 6 5)。

There are 2 problems with the output from this. First, the first line is not output, although from experimentation I know that it DOES read it in. Second, after the last 2 lines are read in and output (as desired), the program displays everything in the last line EXCEPT the first name (in this case the last thing it prints is Bowie 6 5).

有人可以使用这个简单的例子来说明当从文件中读取多行时getline函数如何工作? (我甚至不知道这是最好的方式,但它是我所知道的唯一的方式)。这里有一些具体的问题。

Can someone use this simple example to explain how the getline function works when reading in multiple lines from a file? (I don't even know if it's the best way, but it's the only way I know as of yet). Here are some specific questions.

首先,while循环条件getline(inFile,firstName)是否返回一个布尔值?如果是这样,如果我还没有给firstName一个值,它怎么可能是真的(即如何while循环开始)?是否是程序读取第一行,如果有一些东西,那么它执行while循环,但从第二行开始,因为它已经使用第一个检查内容?

First, does the while loop conditional getline(inFile, firstName) return a boolean? If so, how can it be true (i.e. how can the while loop start) if I haven't given firstName a value yet? Is it the case that the program reads the first line and if there's something there, then it executes the while loop, but starting with the second line, because it already used the first to check for content?

其次,如果firstName有一个值,并且该值是第一行的名字(在这种情况下为Bob),为什么不是第一线输出?

Second, if firstName does have a value, and if that value is the first name on the first line ("Bob" in this case), why isn't the first line output at all? I've been racking my brain trying to figure out where it went to.

第三,在程序读入并显示最后两行之后,程序移动到下一行,只遇到空白,对不对?那么firstName的值是什么呢?它是空白,还是仍然是大卫?如果它为空,为什么while循环再次执行?但是如果它是大卫,那么为什么程序不会与其他人一起输出该值?

Third, after the program reads in and displays the last two lines, the program moves to the next line and encounters nothing but blanks, right? Then what would be the value of firstName? Would it be blank, or would it still be "David"? If it's blank, why does the while loop execute again? But if it's "David", then why does the program not output that value along with the others?

Btw,我正在使用教科书(不是家庭作业),它涵盖getline,但不适用于多行。但是,然后练习涉及多行,所以我有点失去了。

Btw, I am working out of a textbook (not for homework), and it covers getline, but not for multiple lines. But then the exercises involve multiple lines, so I'm a bit lost.

推荐答案

您尝试读取每行两次。

You are trying to read each line twice.

while (getline(inFile, firstName)) // reads the line
    {
        // reads the next line and overwrites firstName!
        inFile >> firstName >> lastName >> num1 >> num2;

更改为:

while ( inFile >> firstName >> lastName >> num1 >> num2 )
{
  fullName = firstName + " " + lastName;
  cout << fullName << " " << num1 << " " << num2 << endl;
}

编辑:回答您的问题:

getline()如何工作?

读取整行,直到\\\
字符或指定的分隔字符。 http://www.cplusplus.com/reference/string/string/ getline /?kw = getline

读取行之后,控件转到文件中的嵌套行。

此外,如果读取操作成功,则返回布尔值true,否则返回false。

After reading the line, the control goes to the nest line in the file.
Also, it returns a boolean value of true if the read operation was successful, else false.

默认情况下,提取运算符会截断所有空格。它还返回一个对应于操作是否成功的布尔值。

The extraction operator truncates on all whitespaces by default. It also returns a boolean value corresponding to whether the operation was successful or not.

这篇关于使用getline()从文件读取多行,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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