C fscanf 让我重复行

标签 c linux pid scanf ps

我试图通过重定向“ps”命令的输出来显示每个进程的详细信息。我已将 ps 输出重定向到一个文件。我正在读取它并使用 fscanf 显示 Pid 和命令。但是我看到我得到了一些重复的行,在某些情况下,Pids 的值为负数。

void TR69_DM_GetProcDetails(char *FileName, int32_t *ProcCount, struct ProcessInfo *ProcVar)
{
    char CharBuf[BUF_SIZE] ;
    int32_t i = 0;

    int Pid;
    char Cmd[100];

    FILE *FileDesc;
    FileDesc = fopen (FileName, "r");

    printf("\nFile pointer %ld\n", ftell(FileDesc));
    fscanf (FileDesc, "%[^\n]\n", CharBuf);
    puts(CharBuf);

    printf("\nFile pointer %ld\n", ftell(FileDesc));

    while ( EOF != fscanf (FileDesc, "%*s %lu %*d %*d %*s %*s %*s %s", &Pid, Cmd))
    {
        printf("\n PID is %d and cmd is %s", Pid, Cmd);
    //  i++;
    }

}

我得到以下输出示例:

 PID is 2104 and cmd is /usr/lib/indicator-datetime/indicator-datetime-service
 PID is 2107 and cmd is /usr/lib/indicator-messages/indicator-messages-service
 PID is 2114 and cmd is /usr/lib/indicator-session/indicator-session-service
 PID is 2134 and cmd is /usr/lib/geoclue/geoclue-master
 PID is 2142 and cmd is gnome-screensaver
 PID is 2145 and cmd is /usr/lib/gnome-disk-utility/gdu-notification-daemon
 PID is 2342 and cmd is /usr/bin/python
 PID is 2342 and cmd is /usr/bin/python
 PID is 4316 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is -4 and cmd is /sbin/dhclient
 PID is 4444 and cmd is /usr/sbin/ntpd
 PID is 4444 and cmd is /usr/sbin/ntpd
 PID is 4444 and cmd is /usr/sbin/ntpd
 PID is 4444 and cmd is /usr/sbin/ntpd
 PID is 113 and cmd is /usr/sbin/ntpd
 PID is 113 and cmd is /usr/sbin/ntpd
 PID is 5411 and cmd is sshd:
 PID is 5411 and cmd is sshd:
 PID is 5411 and cmd is sshd:
 PID is 5584 and cmd is sshd:
 PID is 5584 and cmd is sshd:
 PID is 5585 and cmd is -bash
 PID is 6790 and cmd is /usr/lib/firefox-4.0.1/firefox-bin
 PID is 7007 and cmd is /usr/lib/firefox-4.0.1/plugin-container
 PID is 7007 and cmd is /usr/lib/firefox-4.0.1/plugin-container
 PID is 7007 and cmd is /usr/lib/firefox-4.0.1/plugin-container
 PID is 6790 and cmd is /usr/lib/firefox-4.0.1/plugin-container
 PID is 6790 and cmd is /usr/lib/firefox-4.0.1/plugin-container
 PID is 6790 and cmd is /usr/lib/firefox-4.0.1/plugin-container
 PID is 7023 and cmd is /usr/lib/nspluginwrapper/i386/linux/npviewer.bin
 PID is 7023 and cmd is /usr/lib/nspluginwrapper/i386/linux/npviewer.bin
 PID is 7023 and cmd is /usr/lib/nspluginwrapper/i386/linux/npviewer.bin
 PID is 7023 and cmd is /usr/lib/nspluginwrapper/i386/linux/npviewer.bin
 PID is 7023 and cmd is /usr/lib/nspluginwrapper/i386/linux/npviewer.bin
 PID is 8390 and cmd is man
 PID is 8390 and cmd is man
 PID is 8402 and cmd is pager
 PID is 8402 and cmd is pager
 PID is 8827 and cmd is ping
 PID is 8827 and cmd is ping
 PID is 8869 and cmd is bash
 PID is 8907 and cmd is bash
 PID is 12637 and cmd is cat
 PID is 12637 and cmd is cat
 PID is 15164 and cmd is tftp
 PID is 15185 and cmd is vim
 PID is 15185 and cmd is vim
 PID is 16827 and cmd is telnet
 PID is 16827 and cmd is telnet
 PID is 17117 and cmd is sshd:
 PID is 17117 and cmd is sshd:
 PID is 17117 and cmd is sshd:
 PID is 17226 and cmd is sshd:
 PID is 17226 and cmd is sshd:
 PID is 17227 and cmd is -sh
 PID is 18120 and cmd is su
 PID is 18127 and cmd is bash
 PID is 18896 and cmd is sshd:
 PID is 18896 and cmd is sshd:
 PID is 18896 and cmd is sshd:
 PID is 19069 and cmd is sshd:
 PID is 19069 and cmd is sshd:
 PID is 19070 and cmd is -sh
 PID is 20313 and cmd is gedit
 PID is 20313 and cmd is gedit
 PID is 20313 and cmd is gedit
 PID is 20334 and cmd is ssh
 PID is 20334 and cmd is ssh
 PID is 20337 and cmd is [kworker/1:1]
 PID is 20341 and cmd is su
 PID is 20349 and cmd is bash
 PID is 20371 and cmd is [kworker/0:1]
 PID is 20375 and cmd is [kworker/0:2]
 PID is 20377 and cmd is [kworker/1:0]
 PID is 21342 and cmd is [kjournald]
 PID is 23384 and cmd is /opt/Adobe/Reader9/Reader/intellinux/bin/acroread
 PID is 23384 and cmd is /opt/Adobe/Reader9/Reader/intellinux/bin/acroread
 PID is 23495 and cmd is gnome-terminal
 PID is 23498 and cmd is gnome-pty-helper
 PID is 23499 and cmd is bash
 PID is 26446 and cmd is ssh
 PID is 26446 and cmd is ssh
 PID is 26733 and cmd is sshd:
 PID is 26733 and cmd is sshd:
 PID is 26843 and cmd is -bash
 PID is 26943 and cmd is sshd:
 PID is 26943 and cmd is sshd:
 PID is 27036 and cmd is sshd:
 PID is 27036 and cmd is sshd:
 PID is 27036 and cmd is sshd:
 PID is 27052 and cmd is /usr/lib/openssh/sftp-server
 PID is 27149 and cmd is sshd:
 PID is 27149 and cmd is sshd:
 PID is 27150 and cmd is -bash
 PID is 27734 and cmd is ssh
 PID is 27734 and cmd is ssh
 PID is 28265 and cmd is bin/TR69_DM
 PID is 0 and cmd is bin/TR69_DM
 PID is 28266 and cmd is ps
 PID is 28266 and cmd is ps
 PID is 29510 and cmd is su
 PID is 29510 and cmd is su
 PID is 29517 and cmd is bash
 PID is 29951 and cmd is minicom
 PID is 30056 and cmd is bash
 PID is 30293 and cmd is bash
 PID is 30329 and cmd is ssh
 PID is 30329 and cmd is ssh
 PID is 30597 and cmd is bash
 PID is 30632 and cmd is ssh
 PID is 30632 and cmd is ssh
 PID is 31508 and cmd is bash

可能出了什么问题?

最佳答案

fscanf()返回分配的数量。检查 EOF 是不够的,因为一些赋值可能已经完成而其余的没有,这意味着后续代码将处理陈旧或未初始化的值。以下循环将在格式异常时在第一行停止处理并避免陈旧的值:

while ( 2 == fscanf (FileDesc, "%*s %lu %*d %*d %*s %*s %*s %s", &Pid, Cmd))
{
}

改进的解决方案将使用 fgets()读取一行然后使用 sscanf()解析读取的行。这将允许您检测 EOF 并允许多次尝试使用不同格式解析该行:

char line[1024];
while (fgets(line, sizeof(line), stdin))
{
    if (2 == sscanf (line, "%*s %lu %*d %*d %*s %*s %*s %s", &Pid, Cmd))
    {
    }
    else if (2 == sscanf (line, "some-other-format", &Pid, Cmd))
    {
    }
    else
    {
        fprintf(stderr, "Unable to parse '%s'\n", line);
    }
}

此外,通过指定 sscanf() 应该写入 Cmd 的最大字符数来防止潜在的缓冲区溢出。这应该比 Cmd 的大小小一,为终止空字符留出空间。例如:

if (2 == sscanf(line, "%lu %255s", &Pid, Cmd))

关于C fscanf 让我重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21933108/

相关文章:

c - 参数1.c :4:6: note: expected ‘double *’ but argument is of type ‘double’

c - "pid = fork();"如何在子进程中分配 "pid = 0"?

c - 使用两个循环体还是一个(结果相同)?

c - 自动交互编程

c++ - Qt Assistant 在 Linux 中因启用远程控制选项而失败

c++ - 以非 root 用户身份调用 mount() 系统调用

c - 在 Linux 中,如何在新获取的子进程退出之前了解它们?

linux - 我的 linux bash $$ 不匹配/proc/self 链接?

c - 将 toupper 与 C 中的结构一起使用

python - 是否可以在 Python 中更改父进程的环境?