读取命令结果错误(fgets)

发布于 2024-10-18 05:17:13 字数 4946 浏览 1 评论 0原文

我编写了一个带有按钮的简单可可应用程序。

单击按钮时,会触发计时器:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾冷 2024-10-25 05:17:13

从联机帮助页(http://pubs.opengroup.org/onlinepubs/007908799/xsh/fgets.html):

成功完成后,fgets()
返回 s.如果流位于
end-of-file,文件结束指示符
设置流并使用 fgets()
返回一个空指针。如果读
发生错误时,错误指示器为
流已设置,fgets() 返回一个
空指针并将 errno 设置为
指出错误。

在文件末尾,它返回 null,如果文件少于 4096 个字符,它将在第一次迭代时达到 EOF,因此将无法到达 isFgetsOK = YES;。 Strike>

抱歉,误解了...如果调用 fgets 时流at EOF,则返回 null,如果在调用过程中达到 EOF,则不会返回 null...^^;

From the manpage (http://pubs.opengroup.org/onlinepubs/007908799/xsh/fgets.html):

Upon successful completion, fgets()
returns s. If the stream is at
end-of-file, the end-of-file indicator
for the stream is set and fgets()
returns a null pointer. If a read
error occurs, the error indicator for
the stream is set, fgets() returns a
null pointer and sets errno to
indicate the error.

On end of file, it returns null, and if the file is less than 4096 characters, it would hit EOF on the first iteration, so isFgetsOK = 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... ^^;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文