为简化起见,我尝试使用 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 甚至没有尝试考虑它?
(谢谢 !)