確かにあなたがfseek
を使用して、このような何か試すことができます。もう一つの方法は、このように、fgetpos
とfsetpos
を使用することです
fp = fopen("test-seeking.txt", "r");
while ((fgets(line, BUFMAX, fp))) {
// Get the next line
fgets(nextline, BUFMAX, fp);
// Get the length of nextline
int nextline_len = strlen(nextline);
// Move the file index back to the previous line
fseek(fp, -nextline_len, SEEK_CUR); // Notice the - before nextline_len!
printf("Current line starts with: %-3.3s/Next line starts with %-3.3s\n", line, nextline);
}
を:
fp = fopen("test-seeking.txt", "r");
while ((fgets(line, BUFMAX, fp))) {
// pos contains the information needed from
// the stream's position indicator to restore
// the stream to its current position.
fpos_t pos;
// Get the current position
fgetpos(fp, &pos);
// Get the next line
fgets(nextline, BUFMAX, fp);
// Restore the position
fsetpos(fp, &pos);
printf("Current line starts with: %-3.3s/Next line starts with %-3.3s\n", line, nextline);
}
'fgetc'と' ungetc'を使うか、次の行だけを読み込みます。 –
1行を読み取った後、1行を読み取り、2深度バッファを使用します。バッファの最初の行は現在の行で、もう1行は次の行です。 –
WINAPIにはPeekシリーズの機能があります。 – iBug