我用一个按钮写了一个简单的可可应用程序。
单击该按钮时,将触发计时器:
[NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(readCommandResult:)
userInfo:nil
repeats:YES];
这是计时器:
-(void)readCommandResult:(NSTimer *) timer
{
char ps_cmd[256] = {"ls"};
BOOL isFgetsOK = NO;
FILE *fp = popen(ps_cmd, "r");
if (fp) {
char line[4096];
while (line == fgets(line, 4096, fp)) {
isFgetsOK = YES;
}
pclose(fp);
}
else {
NSLog(@"popen error");
}
if (!isFgetsOK) {
NSLog(@"fgets error");
}
}
当我运行应用程序时,我有时会得到“fgets错误”, 似乎“line == fgets(line,4096,fp))”失败但是我 不是为什么?
有些人可以向我解释为什么我在这里得到“fgets错误” 有时随机??
PS: 我试图改变comamnd(从“ls”到“ps”); 在终端,我得到了结果:
PID TTY TIME CMD
744 ttys000 0:00.02 -bash
计时器中的新代码:
-(void)readCommandResult:(NSTimer *) timer
{
char ps_cmd[256] = {"ps"};
BOOL isFgetsOK = NO;
FILE *fp = popen(ps_cmd, "r");
if (fp) {
char line[4096];
while (line == fgets(line, 4096, fp)) {
NSLog(@"length:%d line:%s", strlen(line), line);
isFgetsOK = YES;
}
pclose(fp);
}
else {
NSLog(@"popen error");
}
if (!isFgetsOK) {
NSLog(@"fgets error");
}
}
我得到了日志:
19:56:23.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:23.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:24.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:24.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:25.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:25.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:26.780 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:26.781 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:27.782 TopDemo[856:a0f] fgets error
19:56:28.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:28.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:29.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:29.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:30.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:30.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:31.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:31.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:32.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:32.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:33.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:33.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:34.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:34.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:35.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:35.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:36.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:36.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:37.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:37.783 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:38.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:38.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:39.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:39.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:40.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:40.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:41.782 TopDemo[856:a0f] fgets error
19:56:42.783 TopDemo[856:a0f] fgets error
19:56:43.782 TopDemo[856:a0f] fgets error
19:56:44.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:44.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:45.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:45.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:46.782 TopDemo[856:a0f] fgets error
19:56:47.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:47.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
19:56:48.781 TopDemo[856:a0f] length:29 line: PID TTY TIME CMD
19:56:48.782 TopDemo[856:a0f] length:31 line: 744 ttys000 0:00.02 -bash
我尝试多次运行应用程序并出现“fgets错误” 随机...
答案 0 :(得分:0)
从联机帮助页(http://pubs.opengroup.org/onlinepubs/007908799/xsh/fgets.html):
成功完成后,fgets() 返回s。如果流是在 文件结束,文件结束指示符 为流设置和fgets() 返回一个空指针。如果阅读 发生错误,错误指示为 设置了流,fgets()返回一个 空指针并将errno设置为 表明错误。
在文件末尾,它返回null,如果文件小于4096个字符,它将在第一次迭代时命中EOF,因此不会到达 < / p>
isFgetsOK = YES;
。
抱歉,误解了......如果在调用fgets时>>