从一个持续更新的文件中读取内容

7

我正在写一些C代码来处理文件中的数据,但我刚刚得知该文件将不断地添加内容(大约每秒钟1次,也可能更快)。因此,我想知道如何在文件被添加时继续读取它。当我读到文件结尾时,等待下一行被添加,然后再处理它。然后再等待并处理,如此往复。我的代码类似于:

while(1){
    fgets(line, sizeof(line), file);
    while(line == NULL){
       //wait ?  then try to read again?
    }
    //tokenize line and do my stuff here
}

我曾尝试使用inotify,但是一无所获。有没有人能提供一些建议?


整行可能没有被写入,所以你可能需要继续阅读,直到找到“\n”。除非你需要更高级的操作,否则我建议等待一段固定时间后再尝试读取。 - Jesus Ramos
首先,该文件包含大约46k行需要立即处理,然后该文件将以大约1行/秒的速度更新。所以我使用while循环,最终当fgets只获取到一个空行时,我就到了一个点。那么,如果我在那里等待,然后手动在文件中插入一个新行并保存它,fgets如何读取新输入的行?我想我有些困惑。 - Matthew The Terrible
2
你的内部循环不应该测试“line”是否为空。应该更像这样:while (fgets(line, sizeof(line), file) != 0) { process(line); } ...nanosleep?...; clearerr(file); (然后它将循环执行 while(1) 循环以尝试进行下一次读取)。 - Jonathan Leffler
Jonathan - 这很有道理。谢谢你。那我仅在回到 while(1) 循环时,我必须关闭文件?然后重新打开并 fseek 到之前留下的位置?对吗? - Matthew The Terrible
2
你可以关闭并重新打开文件,并在遇到EOF之前寻找到你的位置,但是我提到的clearerr(file);会清除流中的EOF和错误位,以便它将尝试从文件中读取更多数据(而无需关闭、重新打开和重新定位文件中的当前位置)。 - Jonathan Leffler
显示剩余2条评论
4个回答

5
最有效的方法是使用inotify,直接方式是直接使用read()系统调用。
使用inotify 以下代码可能会对您有所帮助,在Debian 7.0,GCC 4.7上运行良好:
/*This is the sample program to notify us for the file creation and file deletion takes place in “/tmp/test_inotify” file*/
// Modified from: http://www.thegeekstuff.com/2010/04/inotify-c-program-example/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/inotify.h>

#define EVENT_SIZE  ( sizeof (struct inotify_event) )
#define EVENT_BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 ) )

int main( )
{
  int length, i = 0;
  int fd;
  int wd;
  char buffer[EVENT_BUF_LEN];

  /*creating the INOTIFY instance*/
  fd = inotify_init();
  /*checking for error*/
  if ( fd < 0 ) {
    perror( "inotify_init error" );
  }

  /* adding the “/tmp/test_inotify” test into watch list. Here, 
   * the suggestion is to validate the existence of the 
   * directory before adding into monitoring list.
   */
  wd = inotify_add_watch( fd, "/tmp/test_inotify", IN_CREATE | IN_DELETE | IN_ACCESS | IN_MODIFY | IN_OPEN );

  /* read to determine the event change happens on “/tmp/test_inotify” file. 
   * Actually this read blocks until the change event occurs
   */ 
  length = read( fd, buffer, EVENT_BUF_LEN ); 
  /* checking for error */
  if ( length < 0 ) {
    perror( "read" );
  }  

  /* actually read return the list of change events happens. 
   *  Here, read the change event one by one and process it accordingly.
   */
  while ( i < length ) {
    struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
    if( event->len == 0) {
      // For a single file watching, the event->name is empty, and event->len = 0
      printf(" Single file watching event happened\n");
    } else if ( event->len ) {
      if ( event->mask & IN_CREATE ) {
        if ( event->mask & IN_ISDIR ) {
          printf( "New directory %s created.\n", event->name );
        } else {
          printf( "New file %s created.\n", event->name );
        }
      } else if ( event->mask & IN_DELETE ) {
        if ( event->mask & IN_ISDIR ) {
          printf( "Directory %s deleted.\n", event->name );
        } else {
          printf( "File %s deleted.\n", event->name );
        }
      } else if( event->mask & IN_ACCESS ) {
        if ( event->mask & IN_ISDIR ) {
          printf( "Directory %s accessed.\n", event->name );
        } else {
      printf(" File %s accessed. \n", event->name );
        }
      } else if( event->mask & IN_MODIFY ) {
        if ( event->mask & IN_ISDIR ) {
          printf( "Directory %s modified.\n", event->name );
        } else {
      printf(" File %s modified. \n", event->name );
        }
      } else if( event->mask & IN_OPEN ) {
        if ( event->mask & IN_ISDIR ) {
          printf( "Directory %s opened.\n", event->name );
        } else {
      printf(" File %s opened. \n", event->name );
        }
      } else {
    printf( "Directory or File is accessed by other mode\n");
      }
    }
    i += EVENT_SIZE + event->len;
  }

  /* removing the “/tmp/test_inotify” directory from the watch list. */
  inotify_rm_watch( fd, wd );

  /* closing the INOTIFY instance */
  close( fd );

}

当运行以上程序时,您可以通过创建名为/tmp/test_inotify的文件或目录来测试它。
详细解释请点击这里

使用read系统调用

如果一个文件已经打开,并且已经读取到当前文件大小的末尾,则read()系统调用将返回0。如果某个写入者随后向此文件写入了N字节,那么read()将返回min(N, buffersize)
因此,它适用于您的情况。以下是代码示例。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

typedef int FD ;

int main() {
  FD filed = open("/tmp/test_inotify", O_RDWR );
  char buf[128];

  if( !filed ) {
    printf("Openfile error\n");
    exit(-1);
  }

  int nbytes;
  while(1) {
    nbytes = read(filed, buf, 16);
    printf("read %d bytes from file.\n", nbytes);
    if(nbytes > 0) {
      split_buffer_by_newline(buf); // split buffer by new line.
    }
    sleep(1);
  }
  return 0;
}

参考资料


1
在普通文件上使用read()的问题在于当读取位置到达文件末尾时,它会立即返回0。因此,在尝试新的读取之前,您必须进行某种等待。这与在管道、套接字或FIFO上的读取形成对比,后者将阻塞等待更多数据到达。inotify信息可能是处理它的最佳方式。或者循环和睡眠... - Jonathan Leffler
2
你似乎在链接的网站中包含了大量的内容。如果您提供适当的归属(就像您在这里所做的那样),则可以包含一些内容,但通常不适合将其作为答案的主要部分。请考虑尝试添加更多自己的内容?(我知道您正在进行编辑,也许正在做这件事。) - Andrew Barber
感谢您的评论,我会尝试在下次发布时发表自己的答案。以下是这个答案的解释:1.虽然代码是从参考资料中复制的,但原始代码无法工作,我修改并更新了约20%的代码。2.我将改进并添加新内容到这个答案中。 - Kun Ling
这种方法如何用于外部命令?例如,需要监视在几秒钟后打印某些内容的外部命令输出。 - Yougeshwar Khatri
感谢@KunLing的帖子。我已经尝试了您发布的示例。我已将其更改为我的本地机器路径。当我创建新文件时,只有一次会创建日志文件,之后就会退出。我正在尝试验证在该目录中连续创建文件的列表。 - GNK

0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int
main()
{

    char            ch;
    FILE           *fp;
    long int        nbytes_read = 0;
    char            str       [128];
    int             j = 0;
    int             first_time = 1;
    memset(str, '\0', 128);
    fp = fopen("file.txt", "r");
    while (1) {
            if (first_time != 1) {
                    fp = fopen("file.txt", "r");
                    fseek(fp, nbytes_read, SEEK_SET);
                    sleep(10);

            }
            if (fp != NULL) {
                    while ((ch = fgetc(fp)) != EOF) {
                            if (ch == '\n') {
                                    str[j++] = ch;
                                    printf("%s", str);
                                    memset(str, '\0', 128);
                                    j = 0;
                            } else {
                                    str[j++] = ch;
                            }
                            nbytes_read++;


                    }
                    //printf("%ld\n", nbytes_read);
                    first_time = 0;
            }
            fclose(fp);
    }
    return 0;
}

4
虽然这段代码可能回答了问题,但是提供解释它是如何解决问题的,以及为什么能够解决问题,可以增加其长期价值。 - L_J

-1
你可以使用 select() 函数并传入 fileno(file) 作为文件描述符。当你可以从该文件中读取数据时,select 函数将返回或者在超时后返回。

1
POSIX中select()的定义是:与常规文件相关联的文件描述符应始终选择准备好读取、准备好写入和错误条件。 - Jonathan Leffler

-1

使用 select 可以是一个不错的选择,但如果您不想使用它,可以在读取值之前添加一小段毫秒级的延迟。


1
POSIX中select()的定义是:与常规文件相关联的文件描述符应始终选择准备好读取、准备好写入和错误条件。 - Jonathan Leffler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接