0
    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

I want to write a C program which will only store "Interface Name" in an array, for example the output should be like

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

I wrote a simple program to achieve this, but I'm not getting any suitable output.

NOTE: In the code, I am just printing the required data instead of storing it in an array.

#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;
}
Guy
  • 41
  • 10
Rob
  • 169
  • 2
  • 4
  • 15
  • For the show input, what output do you get? What output did you expect? Why aren't you skipping over the two first lines (the table header)? Are the column widths fixed? Or can the width of the columns differ between different runs of the command? – Some programmer dude Aug 23 '16 at 05:38
  • they are not required. All i want is the list of interface name present on my computer. I suppose it is fixed, but i don't know how to leverage that. – Rob Aug 23 '16 at 05:39
  • Then why are you using this command and parse a text file, instead of using the Windows API to get a list directly? – Some programmer dude Aug 23 '16 at 05:40
  • Which Windows API? Is it getinterfaceinfo()?? – Rob Aug 23 '16 at 05:41

1 Answers1

0

See Creating a Child Process with Redirected Input and Output on MSDN

Alternatively, you can use _popen for simple capture of output.

BlackMamba
  • 10,054
  • 7
  • 44
  • 67