读取命令结果错误(fgets)
我编写了一个带有按钮的简单可可应用程序。
单击按钮时,会触发计时器:
[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 错误”吗 有时随机??
附: 我尝试更改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 错误” 随机...
I wrote a simple cocoa app with a button.
When the button is clicked, a timer is triggered:
[NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(readCommandResult:)
userInfo:nil
repeats:YES];
And here is the timer:
-(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");
}
}
When I run the app, I will get the "fgets error" sometimes,
it seems like "line == fgets(line, 4096, fp))" is failed but I
don't why?
Could some please explain it to me why I get a "fgets error" here
sometimes randomly??
PS:
I tried to change the comamnd (from "ls" to "ps");
In the terminal, I got the result:
PID TTY TIME CMD
744 ttys000 0:00.02 -bash
And the new code in the timer:
-(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");
}
}
And I got the log:
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
And I tried to run the app several times and the "fgets error" come out
randomly...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从联机帮助页(http://pubs.opengroup.org/onlinepubs/007908799/xsh/fgets.html):
在文件末尾,它返回 null,如果文件少于 4096 个字符,它将在第一次迭代时达到 EOF,因此将无法到达isFgetsOK = YES;
。 Strike>抱歉,误解了...如果调用 fgets 时流at EOF,则返回 null,如果在调用过程中达到 EOF,则不会返回 null...^^;
From the manpage (http://pubs.opengroup.org/onlinepubs/007908799/xsh/fgets.html):
On end of file, it returns null, and if the file is less than 4096 characters, it would hit EOF on the first iteration, soisFgetsOK = YES;
would not be reached.Sorry, misinterpreted... It returns null if the stream is at EOF when fgets is called, not if it reaches EOF during the call... ^^;