c - 从不断更新的文件中读取

标签 c linux file-io

我正在编写一些 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,但我一无所获。有人有什么建议吗?

最佳答案

最高效的方法是使用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 的文件或目录来测试它。

可以找到详细的解释here

使用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;
}

引用

关于c - 从不断更新的文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55504494/

相关文章:

linux - 函数内的变量-Linux Shell Script

java - 编辑 JTable 行

java - 用 C++ 将 float 写入二进制文件 |等效于 Java 的 DataOutputStream.writeFloat()

c - Linux 中的软中断处理时序与硬中断处理时序

c - 指针持有地址以外的东西? [C]

c - getpwnam_r() 是否需要可重入?

c - 在 C 中读取文件 - 范围和上下文问题

c - 包括带有大量全局变量的 C 头文件

c - 非 NULL 用户输入

linux - glXSwapIntervalSGI 段错误