事實上,你可以使用fseek
和嘗試是這樣的:
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);
}
另一種方法是使用fgetpos
和fsetpos
,像這樣:
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行,然後讀取一行並使用2深度緩衝區。緩衝區中的第一行是當前行,另一行是下一行。 –
WINAPI具有Peek系列功能。 – iBug