ファイルから読み込み、宿題のコマンドライン引数からデータを解析する作業をしています。私は壁にぶつかり、何が問題なのか分からず、私が逃していることについていくつかアドバイスを受けることができればと願っています。ファイルから読み込むと予期しない出力が発生する
データファイルはこのように構成されています。最初の行には、合計行数があります。それ以降の各行については、|で区切られた文字列です。キャラクター。私は '|'文字列を部分文字列に分割したいからです。
入力ファイルの例を示します。
3
league of legends|Teemo|Master Yi|Vayne
apple|samsung|smart phone|smart watch
overwatch|d.va|junkrat|Reinhart
ここは私のコードです。
int main(int argc, char* const argv[])
{
//change string to char* so I can check through each char to see if the
//thing I read in is '|' character.
String Data = (argv[1]);
ifstream fin (Data.c_str());
//check whether the file is open.
if (!fin.is_open())
{
cout << "Could not open file" << endl;
}
else
{
int dataLines;
char dataBuffer[100];
//The first integer I read in will be how many lines I will loop through
fin >> dataLines;
//ignore the new line character and do not include it in the count of
//dataLines.
fin.ignore();
//use noskipws so I can recognize whitespaces.
fin >> noskipws >> dataBuffer;
//TEST CODE: COMMENTED OUT FOR NOW.
//cout<<dataBuffer<<endl;
//loop for the number of lines
for(int i = 0; i < dataLines; i++)
{
fin.getline(dataBuffer, 100);
//print the buffer for checking
cout<<dataBuffer<<endl;
}
}
//close the file.
fin.close();
return 0;
}
結果はこのように見えることになっています。
league of legends|Teemo|Master Yi|Vayne
apple|samsung|smart phone|smart watch
overwatch|d.va|junkrat|Reinhart
実際の結果は、バッファがなくなってから私が読んで、この
of legends|Teemo|Master Yi|Vayne
apple|samsung|smart phone|smart watch
overwatch|d.va|junkrat|Reinhart
最初の単語のように見えます。 「リーグ」は欠けているもので、テストコードを自分のコードで指定された場所に挿入することで問題を確認しようとしました。与えられたテストコードでは、私の出力は
league
of legends|Teemo|Master Yi|Vayne
apple|samsung|smart phone|smart watch
overwatch|d.va|junkrat|Reinhart
あるので問題は間noskipwsとデータラインをループforloopでファイルを読み込むことです。前の私のバッファーはリーグです。しかし、ループに入るとすぐに、それは渡され、のすぐにになります。
私はここで何が欠けていますか?考えられる解決策は何でしょうか?
fin >> noskipws >> dataBuffer;
はRHEL 7.1
正しい。しかし、理由を説明するのに本当に便利です。 – user4581301