在 C 上使用 sprintf 和系统函数时为空文件
Empty file when using sprintf and system function on C
我想在文件文本中保存一些信息,我写了这个程序:
#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 }' | cut -d'.' -f1-3 | awk '{print \".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
系统命令returns这个:
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.我得到一个空文件
问题:代码正在将 '\n'
读入 buffer[]
,但仍试图将其作为命令的一部分。需要trim缓冲区。 *** 下面
// Insure file is open
fichier=fopen("ethernet_dns.txt","r");
assert(fichier);
// Use fgets
//memset(&buffer,0,sizeof(buffer));
//fread(buffer,20,1,fichier);
if (fgets(buffer, sizeof buffer, fichier)) {
// lop off potential \n
buffer[strcspn(buffer, "\n")] = '[=10=]'; // ***
printf("buffer is: <%s>\n",buffer);
int n = snprintf(command, sizeof(command), "ping -c 1 -W 1 %s > /tmp/ping_result",
buffer);
printf("command is: <%s>\n",command);
// Only issue command if no problems occurred in snprintf()
if (n > 0 && n < sizeof(command)) system(command);
发布的代码有几个问题
1) 将 ping
的结果输出到标准输出而不是 /tmp/ping_result
2) 无法从 buffer[]
数组
中删除结尾的换行符
下面的代码
1) 清理缩进
2) 修正了代码中的问题
3) 处理调用 fopen()
可能失败的问题
4) 删除不需要的最终语句:return 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void )
{
FILE *fichier;
char buffer[20];
char command[200];
system(" cat /etc/resolv.conf | grep nameserver | awk -F' ' '{print }' | cut -d'.' -f1-3 | awk '{print \".1\"}' > ethernet_dns.txt");
fichier=fopen("ethernet_dns.txt","r");
if( !fichier )
{
perror( "fopen for ethernet_dns.txt failed");
exit( EXIT_FAILURE );
}
// implied else, fopen successful
memset(buffer,0,sizeof(buffer));
size_t len = fread(buffer,1, sizeof(buffer),fichier);
printf( "len is: %lu\n", len );
buffer[len-1] = '[=10=]'; // eliminates trailing newline
printf("buffer is: %s\n",buffer);
snprintf(command,sizeof(command),"ping -c 1 -W 1 ");
strcat( command, buffer);
strcat( command, " > /tmp/ping_result");
printf("command is: %s\n",command);
system(command);
}
我电脑上的结果输出在文件中:/tmp/ping_result
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.046 ms
--- 127.0.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.046/0.046/0.046/0.000 ms
我想在文件文本中保存一些信息,我写了这个程序:
#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 }' | cut -d'.' -f1-3 | awk '{print \".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
系统命令returns这个:
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.我得到一个空文件
问题:代码正在将 '\n'
读入 buffer[]
,但仍试图将其作为命令的一部分。需要trim缓冲区。 *** 下面
// Insure file is open
fichier=fopen("ethernet_dns.txt","r");
assert(fichier);
// Use fgets
//memset(&buffer,0,sizeof(buffer));
//fread(buffer,20,1,fichier);
if (fgets(buffer, sizeof buffer, fichier)) {
// lop off potential \n
buffer[strcspn(buffer, "\n")] = '[=10=]'; // ***
printf("buffer is: <%s>\n",buffer);
int n = snprintf(command, sizeof(command), "ping -c 1 -W 1 %s > /tmp/ping_result",
buffer);
printf("command is: <%s>\n",command);
// Only issue command if no problems occurred in snprintf()
if (n > 0 && n < sizeof(command)) system(command);
发布的代码有几个问题
1) 将 ping
的结果输出到标准输出而不是 /tmp/ping_result
2) 无法从 buffer[]
数组
下面的代码
1) 清理缩进
2) 修正了代码中的问题
3) 处理调用 fopen()
4) 删除不需要的最终语句:return 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void )
{
FILE *fichier;
char buffer[20];
char command[200];
system(" cat /etc/resolv.conf | grep nameserver | awk -F' ' '{print }' | cut -d'.' -f1-3 | awk '{print \".1\"}' > ethernet_dns.txt");
fichier=fopen("ethernet_dns.txt","r");
if( !fichier )
{
perror( "fopen for ethernet_dns.txt failed");
exit( EXIT_FAILURE );
}
// implied else, fopen successful
memset(buffer,0,sizeof(buffer));
size_t len = fread(buffer,1, sizeof(buffer),fichier);
printf( "len is: %lu\n", len );
buffer[len-1] = '[=10=]'; // eliminates trailing newline
printf("buffer is: %s\n",buffer);
snprintf(command,sizeof(command),"ping -c 1 -W 1 ");
strcat( command, buffer);
strcat( command, " > /tmp/ping_result");
printf("command is: %s\n",command);
system(command);
}
我电脑上的结果输出在文件中:/tmp/ping_result
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.046 ms
--- 127.0.1.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.046/0.046/0.046/0.000 ms