特定の行にあるテキスト ファイルからデータを取得しようとしています (1 番目、7 番目、13 番目など - 必要なデータは次の 6 行目に配置されます)。
これまでの私のコードは次のとおりです。
txtfile = "titles.txt";
ifstream txt(txtfile);
const int buffer_size = 80;
char title_buffer[buffer_size];
const int titleLineDiff = 6;
if (txt.is_open())
{
while(!txt.eof())
{
static int counter = 1;
txt.getline(title_buffer, buffer_size);
cout << "Title: \"" << counter << "." << title_buffer << "\"" << endl;
counter++;
//seek to the next title...difference is 6 lines
for(int i = 0; i < titleLineDiff; i++)
txt.getline(title_buffer, 40);
}
}
今、私が作成したこのファイルでうまく動作します:
testONE
two
three
four
five
six
testTWO
bla
そして、「testONE」と「testTWO」を出力しますが、データを含むファイルを開こうとすると、無限ループが発生し、出力は
タイトル: "counter_increasing_number."
テキスト ドキュメントはインターネットからコピーされたものであり、これが読み取りの問題の原因である可能性があります。
これについて何ができますか?
コードを次のように変更しました。
while(getline(txt,title_buffer))
{
static int counter = 1;
//getline(title_buffer, buffer_size);
cout << "Title: \"" << counter << "." << title_buffer << "\"" << endl;
counter++;
//seek to the next title...difference is 6 lines
for(int i = 0; i < titleLineDiff; i++)
{
getline(txt, title_buffer);
}
}
そしてそれはうまくいきました。
誰かが最初にうまくいかなかった理由を説明してもらえますか?