C 这允许正确的同步。您甚至可以将实际的文件内存映射到每个进程中。因此,首先文件包含46k行,我需要立即处理这些行,然后文件将以大约1行/秒的速度更新。所以我做了while循环,最终我得到了一个点,fgets只得到一个空行。因此,如果我在那里等待,然后手动在文
C 这允许正确的同步。您甚至可以将实际的文件内存映射到每个进程中。因此,首先文件包含46k行,我需要立即处理这些行,然后文件将以大约1行/秒的速度更新。所以我做了while循环,最终我得到了一个点,fgets只得到一个空行。因此,如果我在那里等待,然后手动在文,c,linux,file-io,C,Linux,File Io,这允许正确的同步。您甚至可以将实际的文件内存映射到每个进程中。因此,首先文件包含46k行,我需要立即处理这些行,然后文件将以大约1行/秒的速度更新。所以我做了while循环,最终我得到了一个点,fgets只得到一个空行。因此,如果我在那里等待,然后手动在文件中放入新行并保存它,fgets如何读取新输入的行?我想我不仅仅是有点困惑。您的内部循环不应该测试行是否为空。它应该更像:while(fgets(line,sizeof(line),file)!=0){process(line);}…nanos
这允许正确的同步。您甚至可以将实际的文件内存映射到每个进程中。因此,首先文件包含46k行,我需要立即处理这些行,然后文件将以大约1行/秒的速度更新。所以我做了while循环,最终我得到了一个点,fgets只得到一个空行。因此,如果我在那里等待,然后手动在文件中放入新行并保存它,fgets如何读取新输入的行?我想我不仅仅是有点困惑。您的内部循环不应该测试
行
是否为空。它应该更像:while(fgets(line,sizeof(line),file)!=0){process(line);}…nanosleep?。。。;clearerr(文件)
(然后将在循环,而(1)
循环进行下一次尝试读取。您可以关闭并重新打开该文件,并查找以前遇到EOF时的位置,但clearerr(文件);
会清除流中的EOF和错误位,以便尝试从该文件读取更多数据(无需关闭、重新打开和重新定位文件中的当前位置)。in-POSIX的定义是:与常规文件相关联的文件描述符应始终选择true以表示就绪读取、就绪写入和错误条件。in-POSIX的定义是:与常规文件相关联的文件描述符应始终选择true以表示就绪读取、就绪写入和错误条件SIX表示:与常规文件关联的文件描述符应始终选择true以表示就绪读取、就绪写入和错误条件。POSIX中的定义表示:与常规文件关联的文件描述符应始终选择true以表示就绪读取、就绪写入和错误条件。read()
在普通文件上,当读取位置在文件末尾时,它会立即返回0。因此,在尝试新的读取之前,您必须进行某种等待。这与在管道、套接字或FIFO上读取不同,后者会阻止等待更多数据到达。inotify
信息可能是最好的反读取方法AL与它。那个,循环和睡眠…你似乎已经包含了大量的内容从你链接的网站。可以包括一些,只要你提供适当的归因(如你在这里),但这往往是不适合这么多你的答案。请考虑尝试添加更多自己的内容呢?(我知道你现在正在编辑它,也许看起来正是这么做的。)谢谢你的评论,我将尝试在下次发布我自己的答案。以下是对此答案的解释:1.虽然代码是从参考中复制的,但原始代码不起作用,我已经修改和更新了大约20%的代码。2.我将改进并添加新的内容到这个答案中。如何将此方法用于r外部命令?例如,需要监控外部命令输出,在几秒钟后打印出post@KunLing的内容。我尝试过post示例。我已更改到我的本地计算机路径。当我创建新文件时,只有一次创建日志文件,之后它才出来。我正在尝试验证直接在中创建文件的t。read()的问题
在普通文件上,当读取位置在文件末尾时,它会立即返回0。因此,在尝试新的读取之前,您必须进行某种等待。这与在管道、套接字或FIFO上读取不同,后者会阻止等待更多数据到达。inotify
信息可能是最好的反读取方法AL与它。那个,循环和睡眠…你似乎已经包含了大量的内容从你链接的网站。可以包括一些,只要你提供适当的归因(如你在这里),但这往往是不适合这么多你的答案。请考虑尝试添加更多自己的内容呢?(我知道你现在正在编辑它,也许看起来正是这么做的。)谢谢你的评论,我将尝试在下次发布我自己的答案。以下是对此答案的解释:1.虽然代码是从参考中复制的,但原始代码不起作用,我已经修改和更新了大约20%的代码。2.我将改进并添加新的内容到这个答案中。如何将此方法用于r外部命令?例如,需要监控外部命令输出,在几秒钟后打印出post@KunLing的内容。我尝试过post示例。我已更改到我的本地计算机路径。当我创建新文件时,只有一次创建日志文件,之后它才出来。我正在尝试验证直接在中创建文件。虽然此代码可以回答此问题,但提供有关如何以及为什么解决此问题的信息可提高其长期价值。而此代码可以回答此问题,提供有关如何以及为什么解决此问题的信息可提高其长期价值
while(1){
fgets(line, sizeof(line), file);
while(line == NULL){
//wait ? then try to read again?
}
//tokenize line and do my stuff here
}
/*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 );
}
#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;
}
#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;
}