使用C语言从表中提取相关数据

时间:2016-08-23 05:34:26

标签: c arrays string

    C:/>netsh interface show interface

    Admin State    State          Type             Interface Name
    -------------------------------------------------------------------------
    Disabled       Disconnected   Dedicated        Wireless Network Connection 2
    Disabled       Disconnected   Dedicated        Local Area Connection 2
    Enabled        Connected      Dedicated        Wireless Network Connection
    Enabled        Disconnected   Dedicated        Local Area Connection

我想编写一个只在数组中存储“接口名称”的C程序,例如输出应该像

array=['Wireless Network Connection 2','Local Network Connection 2',
'Wireless Network Connection','Local Network Connection']

我写了一个简单的程序来实现这一点,但我没有得到任何合适的输出。

注意:在代码中,我只是打印所需的数据而不是将其存储在数组中。

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>

int main(){
    //FreeConsole();
    system("netsh interface show interface > output.txt");
    FILE *fp;
    fp = fopen("output.txt","r");
    char line[256];
    while(fgets(line, sizeof(line), fp)){
        printf("==>   %s", line);
        int i = 0;
        char *p = strtok(line,"  ");
        while(p != NULL){
            printf("%s\n", p);
            p = strtok(NULL, "  ");
        }
    }
    fclose(fp);
    getch();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

请参阅在MSDN上创建Child Process with Redirected Input and Output

或者,您可以使用_popen来简单捕获输出。