簡単にするために、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」、つまり2行目の最初の数字(コンソールを参照)を除いて、すべてをうまく読み取るようです。
Iteration number 1
1
Iteration number 2
2
Iteration number 3
3
Iteration number 4
5
Iteration number 5
6
したがって、私の質問:getlineがそれを考慮に入れようとさえしないほど(私が推測する)'\ n'の後にこの'4'を作るのはなぜですか?
(ありがとうございました !)