首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

如何实现C语言的文件系统监听

2012-05-15 
怎么实现C语言的文件系统监听我想采用两个线程来实现一个功能,即一个线程可以监听文件夹中的新产生的文件,

怎么实现C语言的文件系统监听
我想采用两个线程来实现一个功能,即一个线程可以监听文件夹中的新产生的文件,并删除已读取的文件,另一个线程对之前的线程取来的数据进行处理,兄弟姐妹帮帮我啊,我怎么实现

[解决办法]
linux下更简单,用户模式下就有设置回调的函数,随便google到的代码,没环境,没调试,自己改改应该能用,这是阻塞版本,非阻塞使用select,自己搜下

C/C++ code
#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <sys/types.h>#include <sys/inotify.h>#define EVENT_SIZE ( sizeof (struct inotify_event) )+#define BUF_LEN ( 1024 * ( EVENT_SIZE 16 ) )int main( int argc, char **argv ){  int length, i = 0;  int fd;  int wd;  char buffer[BUF_LEN];    fd = inotify_init();    if ( fd < 0 ) {     perror( "inotify_init" );  }  wd = inotify_add_watch( fd, "/home/strike",                         IN_MODIFY | IN_CREATE | IN_DELETE );  length = read( fd, buffer, BUF_LEN );  if ( length < 0 ) {     perror( "read" );  }  while ( i < length ) {     struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];          if ( event->len ) {       if ( event->mask & IN_CREATE ) {           if ( event->mask & IN_ISDIR ) {              printf( "The directory %s was created.\n", event->name );           }           else {              printf( "The file %s was created.\n", event->name );           }     }       else if ( event->mask & IN_DELETE ) {           if ( event->mask & IN_ISDIR ) {              printf( "The directory %s was deleted.\n", event->name );           }           else {              printf( "The file %s was deleted.\n", event->name );           }      }      else if ( event->mask & IN_MODIFY ) {          if ( event->mask & IN_ISDIR ) {              printf( "The directory %s was modified.\n", event->name );          }          else {              printf( "The file %s was modified.\n", event->name );         }      }   }    i = EVENT_SIZE event->len;  }  ( void ) inotify_rm_watch( fd, wd );  ( void ) close( fd );  exit( 0 );} 

热点排行