Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我想把字符串写到文件的顶部。但它不起作用_C_Linux_Printf_Fopen_Ping - Fatal编程技术网

C 我想把字符串写到文件的顶部。但它不起作用

C 我想把字符串写到文件的顶部。但它不起作用,c,linux,printf,fopen,ping,C,Linux,Printf,Fopen,Ping,我要写“ping”的结果 首先,我编写命令行,然后..编写ping结果的其余部分 像这样 平-c5-W1 192.168.30.52 PING 192.168.30.52 192.168.30.52 5684字节的数据。 192.168.30.52中的64字节:icmp_seq=1 ttl=64时间=0.368毫秒 192.168.30.52中的64字节:icmp_seq=2 ttl=64时间=0.408毫秒 192.168.30.52中的64字节:icmp_seq=3 ttl=64时间=0.4

我要写“ping”的结果

首先,我编写命令行,然后..编写ping结果的其余部分

像这样

平-c5-W1 192.168.30.52 PING 192.168.30.52 192.168.30.52 5684字节的数据。 192.168.30.52中的64字节:icmp_seq=1 ttl=64时间=0.368毫秒 192.168.30.52中的64字节:icmp_seq=2 ttl=64时间=0.408毫秒 192.168.30.52中的64字节:icmp_seq=3 ttl=64时间=0.400毫秒 192.168.30.52中的64字节:icmp_seq=4 ttl=64时间=0.392毫秒 192.168.30.52中的64字节:icmp_seq=5 ttl=64时间=0.393毫秒

--192.168.30.52平统计-- 发送5个数据包,接收5个,0%数据包丢失,时间3996ms rtt最小值/平均值/最大值/mdev=0.368/0.392/0.408/0.018 ms

但这个源结果是。。。命令行写在文件的末尾

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>

#define FILE_NAME "ping.txt" 
#define doSystem system

void main(void) {
    FILE *fp;
    char cmdBuf[256], fileBuf[256], buffer[256];
    char dst_addr[124] = "192.168.30.52";
    struct in_addr ipaddr;

    ssize_t read;
    size_t len = 0;

    if( !inet_aton(dst_addr, &ipaddr) ) {
        printf("invalid ip address\n");
    } else {
        sprintf(cmdBuf, "ping -c5 -W1 %s > %s", dst_addr, FILE_NAME );
        fp = fopen(FILE_NAME, "a+");
        fprintf(fp , "ping -c5 -W1 %s\n", dst_addr);
        doSystem(cmdBuf);

        fp = fopen(FILE_NAME, "r");
        while(fgets(buffer, 255, (FILE*) fp)) {
            printf("%s", buffer);
        }
    }
}
这个结果是

PING 192.168.30.52 192.168.30.52 5684字节的数据。 192.168.30.52中的64字节:icmp_seq=1 ttl=64时间=0.368毫秒 192.168.30.52中的64字节:icmp_seq=2 ttl=64时间=0.408毫秒 192.168.30.52中的64字节:icmp_seq=3 ttl=64时间=0.400毫秒 192.168.30.52中的64字节:icmp_seq=4 ttl=64时间=0.392毫秒 192.168.30.52中的64字节:icmp_seq=5 ttl=64时间=0.393毫秒

--192.168.30.52平统计-- 发送5个数据包,接收5个,0%数据包丢失,时间3996ms rtt最小值/平均值/最大值/mdev=0.368/0.392/0.408/0.018 ms 平-c5-W1 192.168.30.52

我怎样才能修好它

sprintf(cmdBuf, "ping -c5 -W1 %s >> %s", dst_addr, FILE_NAME );
fp = fopen(FILE_NAME, "w");
fprintf(fp , "ping -c5 -W1 %s\n", dst_addr);
fclose(fp);
doSystem(cmdBuf);
我喜欢这个

它起作用了


感谢所有的评论

文件的输出已完全缓冲。在执行命令之前,需要刷新缓冲区

    sprintf(cmdBuf, "ping -c5 -W1 %s > %s", dst_addr, FILE_NAME );
    fp = fopen(FILE_NAME, "a+");
    fprintf(fp , "ping -c5 -W1 %s\n", dst_addr);
    fflush(fp);
    doSystem(cmdBuf);

在sprintf中,>将文件内容名称替换为w模式。>>的使用与模式相同

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>

#define FILE_NAME "ping.txt" 
#define doSystem system

void main(void)
{
        FILE *fp;
        char cmdBuf[256], fileBuf[256], buffer[256];
        char dst_addr[124] = "192.168.0.6";
        struct in_addr ipaddr;

        ssize_t read;
        size_t len = 0;

        if( !inet_aton(dst_addr, &ipaddr) ) 
        {
                printf("invalid ip address\n");
        }
        else 
        {
           sprintf(cmdBuf, "ping -c5 -W1 %s >> %s", dst_addr, FILE_NAME );
                fp = fopen(FILE_NAME, "a+");
                fprintf(fp , "ping -c5 -W1 %s\n", dst_addr);
                fclose(fp);
                doSystem(cmdBuf);
        }    
}

通过创建新文件,首先写入新行,然后追加旧文件的其余部分。最后,将新名称重命名为旧名称。他说:这是个坏主意;日志文件总是附加的,而不是重写的。在调用doSystem之前,请尝试添加fclose。创建一个临时文件,将所有最新的ping结果写入该文件,然后将旧文件数据附加到临时文件,然后删除旧文件,并将临时文件名重命名为旧文件名。我刚刚尝试在doSystem之前fclose。但是不起作用。!在cmdbuff中为将来的读卡器创建的命令中尝试>>而不是>,之前发生的情况是:当您忘记关闭文件时,缓冲了第一行,在开始时写入了>,在程序结束时,最后刷新了第一行。现在,您可以正确地使用w模式打开以删除最终的上一个内容,关闭文件并在文件末尾正确地附加>>。无关:几十年来不推荐使用void main,应使用int main并将值返回给环境,始终关闭您打开的流是很好的做法…谢谢您的回答!!!!