getline() 与 ifstream 的意外行为
为了简化,我尝试使用 ifstream 类及其 getline() 成员函数读取 CSV 文件的内容。这是这个 CSV 文件:
1,2,3
4,5,6
和代码:
#include <iostream>
#include <typeinfo>
#include <fstream>
using namespace std;
int main() {
char csvLoc[] = "/the_CSV_file_localization/";
ifstream csvFile;
csvFile.open(csvLoc, ifstream::in);
char pStock[5]; //we use a 5-char array just to get rid of unexpected
//size problems, even though each number is of size 1
int i =1; //this will be helpful for the diagnostic
while(csvFile.eof() == 0) {
csvFile.getline(pStock,5,',');
cout << "Iteration number " << i << endl;
cout << *pStock<<endl;
i++;
}
return 0;
}
我期望读取所有数字,因为 getline 应该获取自上次读取以来写入的内容,并在遇到 ',' 或 '\n' 时停止。
但看起来它读取一切都很好,除了“4”,即第二行的第一个数字(参见控制台):
Iteration number 1
1
Iteration number 2
2
Iteration number 3
3
Iteration number 4
5
Iteration number 5
6
因此我的问题:是什么让这个“4”在(我猜)“\n”之后如此具体getline 甚至没有尝试考虑它?
(谢谢 !)
To simplify, I'm trying to read the content of a CSV-file using the ifstream class and its getline() member function. Here is this CSV-file:
1,2,3
4,5,6
And the code:
#include <iostream>
#include <typeinfo>
#include <fstream>
using namespace std;
int main() {
char csvLoc[] = "/the_CSV_file_localization/";
ifstream csvFile;
csvFile.open(csvLoc, ifstream::in);
char pStock[5]; //we use a 5-char array just to get rid of unexpected
//size problems, even though each number is of size 1
int i =1; //this will be helpful for the diagnostic
while(csvFile.eof() == 0) {
csvFile.getline(pStock,5,',');
cout << "Iteration number " << i << endl;
cout << *pStock<<endl;
i++;
}
return 0;
}
I'm expecting all the numbers to be read, since getline is suppose to take what is written since the last reading, and to stop when encountering ',' or '\n'.
But it appears that it reads everything well, EXCEPT '4', i.e. the first number of the second line (cf. console):
Iteration number 1
1
Iteration number 2
2
Iteration number 3
3
Iteration number 4
5
Iteration number 5
6
Thus my question: what makes this '4' after (I guess) the '\n' so specific that getline doesn't even try to take it into account ?
(Thank you !)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在读取逗号分隔的值,因此按顺序读取:
1
,2
,3\n4
,5
, <代码>6。然后,每次打印数组的第一个字符:即
1
、2
、3
、5
、<代码>6。你在期待什么?
顺便说一句,您对
eof
的检查位置错误。您应该检查getline
调用是否成功。在您的特定情况下,它目前不会产生任何影响,因为getline
会在一个操作中读取某些内容并触发 EOF,但一般来说,它可能会在不读取任何内容的情况下失败,并且您当前的循环仍会处理pStock
就像它已成功重新填充一样。更一般地说,像这样的东西会更好:
You are reading comma separated values so in sequence you read:
1
,2
,3\n4
,5
,6
.You then print the first character of the array each time: i.e.
1
,2
,3
,5
,6
.What were you expecting?
Incidentally, your check for
eof
is in the wrong place. You should check whether thegetline
call succeeds. In your particular case it doesn't currently make a difference becausegetline
reads something and triggers EOF all in one action but in general it might fail without reading anything and your current loop would still processpStock
as if it had been repopulated successfully.More generally something like this would be better:
AFAIK 如果您使用终止符参数,
getline()
会读取直到找到分隔符。这意味着在您的情况下,它已读入数组
pSock
,但您只打印第一个字符,因此您只得到3
。AFAIK if you use the terminator parameter,
getline()
reads until it finds the delimiter. Which means that in your case, it has readinto the array
pSock
, but you only print the first character, so you get3
only.您的代码的问题是,当在您的情况下指定分隔符“,”时, getline 使用它并忽略默认分隔符“\n”。如果您想扫描该文件,可以使用标记化功能。
the problem with your code is that getline, when a delimiter is specified, ',' in your case, uses it and ignores the default delimiter '\n'. If you want to scan that file, you can use a tokenization function.