我正在尝试使用C获取我自己的ip地址。
想法是获取“ ifconfig”的输出,将其放在.txt文件中,并提取inet和inet6值。
我尝试将ifconfig输出写入.txt文件进行堆栈:

#include <stdio.h>
#include <stdlib.h>
#include <string>
int main ()
{
    char command[1000];
    strcpy(command, "ifconfig");
    system(command);

    FILE *fp = fopen("textFile.txt", "ab+");

    //... how to write the output of 'system(command)' in the textFile.txt?

    fclose(fp);

    //... how to scraping a file .text in C  ???

    return 0;
}


感谢您的帮助和建议,
斯特凡诺

最佳答案

实际上,您实际上想使用系统调用来完成此操作-而不是运行ifconfig命令。

对于Linux,这里有一个类似的问题:using C code to get same info as ifconfig

(由于ifconfig是Linux命令,所以我假设您是在询问Linux)。

一般要点是使用ioctl()系统调用。

否则,您将分叉您的进程以将其分为两部分,创建一条从子项的输出到父项的输入的管道,并在子项上调用exec以便将其替换为“ ifconfig”。然后,如果要从字符串中获取任何有用的信息,则必须解析该字符串。

关于c - 如何用C语言在文件.txt中附加“ifconfig”?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42931723/

10-09 07:22