我已经读程序日志文件中要做到这一点,我想用select()和read()选择()不会等待
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <fcntl.h>
#include <stdio.h>
int main()
{
int fd = -1;
fd_set fds;
struct timeval tv;
int rc;
char buffer[4096];
char * log_path = "/home/mich/a.txt";
if((fd = open(log_path,O_RDONLY)) == -1)
{
printf("error\n");
return -1;
}
while(1)
{
FD_ZERO(&fds);
FD_SET(fd,&fds);
tv.tv_sec = 2;
tv.tv_usec = 0;
rc = select(fd+1, &fds, NULL, NULL, &tv);
if (rc < 0) {
printf("failed\n");
continue;
} else if (rc > 0 && FD_ISSET(fd,&fds)) {
printf("read\n");
} else {
printf("timeout\n");
continue;
}
while ((my_getline(buffer,sizeof(buffer),fd)) > 0)
{
printf("%s",buffer);
}
}
close(fd);
}
my_getline是使用阅读()的函数。
输出:
read
aaa
read
read
read
read
read
bbb
read
read
read
read
...
,其中aaa和bbb是从读出的文件中的行。
这个程序有什么问题?
你期望你的程序做什么? – ShiDoiSi 2010-09-04 12:39:05
等待选择新行,当他们看到他们阅读。现在看来,选择什么都不做。 – mmatloka 2010-09-04 12:42:41
你为什么要传递“fd + 1”来选择?这听起来不对。 – JesperE 2010-09-04 12:50:58