在linux c 下 怎么取出配置文件中想要的字符串:

2025-05-14 00:22:38
推荐回答(2个)
回答1:

解析文件,获取IP地址的代码如下;

代码:

#include 

#include 

#include 

#include 

#include 

#include 

#define MAXSIZE 80

char buf[MAXSIZE];

char IP_buf[MAXSIZE];

int main(int argc, char * argv[])

{

int fd;

char *delim = "ipaddr";

char *p;

fd = open("./config",O_RDONLY);

if(fd < 0)

{

perror("call to open!");

exit(1);

}

read(fd,buf,MAXSIZE);

p = strstr(buf,delim);

if(p)

{

p = p + strlen(delim);

for(;*p++ == ' ';);

p--;

strcpy(IP_buf,p);

}

printf("IP: %s",IP_buf);

return 0;

}

运行结果如截图,打开的文件就放在当前目录,你自己按你上面的内容创建一个config文件,输入你上面的内容,然后直接执行这个代码。

回答2:

gawk '{print $2}' < config