【问题标题】:How to peek ahead when processing an input file (processing 2 lines)处理输入文件时如何提前查看(处理 2 行)
【发布时间】:2017-08-29 11:41:23
【问题描述】:

当逐行浏览文本文件时,我希望能够向前看下一行并在处理当前行时检查它。我正在使用 C 语言工作。我相信fseek() 或其他类似功能会帮助我解决这个问题,但我不确定也不知道如何使用它们。我想达到以下效果:

    fp = fopen("test-seeking.txt", "r");

    while((fgets(line, BUFMAX, fp))) {
        // Peek over to next line
        nextline = ...;
        printf("Current line starts with: %-3.3s / Next line starts with %-3.3s\n",
               line, nextline);
    }

感谢您的帮助。

【问题讨论】:

  • 使用fgetc 和ungetc 或者直接阅读下一行。
  • 读取 1 行,然后读取 1 行并使用 2 深度缓冲区。缓冲区中的第一行是当前行,另一行是下一行。
  • WINAPI 具有 Peek 系列函数。
  • @iBug 对不起什么?

标签: c file fopen


【解决方案1】:

确实,您可以使用 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);
}

【讨论】:

  • 感谢您的回复!
【解决方案2】:

以下代码的灵感来自@Jean-François Fabrecomment。它将使用一个二维字符数组lineBuffer,用于保存行。第一个读取行写入索引 0 lineBuffer[0],第二行写入lineBuffer[1]。之后,在toggle variablelineSel 的帮助下,这些文字在索引 0 和 1 之间交替。作为最后一步,curLine 指针将设置为nextLine。

因此,您可以在循环中使用 curLine 和 nextLine。 如果您的文件包含以下内容:

line 1
line 2
line 3
...

您将与:

curLine  = "line 1\n"
nextLine = "line 2\n"

curLine  = "line 2\n"
nextLine = "line 3\n"

...

见live example with stdin instead of a file on ideone。

代码:

#include <stdio.h>

#define BUFMAX       256
#define CURLINE      0
#define NEXTLINE     1
#define TOGGLELINE   (CURLINE ^ NEXTLINE)

int main ()
{
   FILE* fp = fopen("test-seeking.txt", "r");

   char lineBuffer[2][BUFMAX];
   char* curLine;
   char* nextLine;
   int lineSel;

   if (fp != NULL)
   {
      if ((curLine = fgets(lineBuffer[CURLINE], BUFMAX, fp)))
      {
         for (lineSel = NEXTLINE;
              (nextLine = fgets(lineBuffer[lineSel], BUFMAX, fp));
              lineSel ^= TOGGLELINE)
         {
            printf("Current line: \"%s\" / Next line \"%s\"\n",
                   curLine, nextLine);

            curLine = nextLine;
         }
      }

      fclose(fp);
   }

   return 0;
}

【讨论】:

  • 感谢您对我的问题 Andre 的回复!我刚刚选择了一个答案作为解决方案。
猜你喜欢
  • 1970-01-01
  • 2016-09-22
  • 1970-01-01
  • 1970-01-01
  • 2016-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-22
相关资源
最近更新 更多