我想在文件文本中保存一些信息,我写了这个程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char *argv[])
{
FILE *fichier;
char buffer[20];
char command[200];
char command1[100];
system(" cat /etc/resolv.conf | grep nameserver | awk -F' ' '{print $2}' | cut -d'.' -f1-3 | awk '{print $1\".1\"}' > ethernet_dns.txt");
fichier=fopen("ethernet_dns.txt","r");
memset(&buffer,0,sizeof(buffer));
fread(buffer,20,1,fichier);
printf("buffer is: %s",buffer);
snprintf(command,sizeof(command),"ping -c 1 -W 1 %s > /tmp/ping_result",buffer);
printf("command is: %s",command);
system(command);
return 0;
}
结果:
buffer is: 127.0.1.1
command is : ping -c 1 -W 1 127.0.1.1
系统命令返回:
PING 127.0.1.1 (127.0.1.1) 56(84) bytes of data.
64 bytes from 127.0.1.1: icmp_seq=1 ttl=64 time=0.115 ms
--- 127.0.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.115/0.115/0.115/0.000 ms
但是当我运行时: cat /tmp/ping_result.I 有一个空文件