问题
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 !)
回答1:
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 the getline
call succeeds. In your particular case it doesn't currently make a difference because getline
reads something and triggers EOF all in one action but in general it might fail without reading anything and your current loop would still process pStock
as if it had been repopulated successfully.
More generally something like this would be better:
while (csvFile.getline(pStock,5,',')) {
cout << "Iteration number " << i << endl;
cout << *pStock<<endl;
i++;
}
回答2:
AFAIK if you use the terminator parameter, getline()
reads until it finds the delimiter. Which means that in your case, it has read
3\n4
into the array pSock
, but you only print the first character, so you get 3
only.
回答3:
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.
来源:https://stackoverflow.com/questions/3511110/unexpected-behaviour-of-getline-with-ifstream