Faylning yuqorisiga string yozishni istayman. lekin u ishni bajarmaydi. (v)

"Ping" ning natijasini yozishni istayman.

birinchi navbatda, buyruq satrini yozaman va keyin .. ping natija qolganini yozish.

shunga o'xshash.

ping -c5 -W1 192.168.30.52
PING 192.168.30.52 (192.168.30.52) 56(84) bytes of data.
64 bytes from 192.168.30.52: icmp_seq=1 ttl=64 time=0.368 ms
64 bytes from 192.168.30.52: icmp_seq=2 ttl=64 time=0.408 ms
64 bytes from 192.168.30.52: icmp_seq=3 ttl=64 time=0.400 ms
64 bytes from 192.168.30.52: icmp_seq=4 ttl=64 time=0.392 ms
64 bytes from 192.168.30.52: icmp_seq=5 ttl=64 time=0.393 ms

--- 192.168.30.52 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 3996ms
rtt min/avg/max/mdev = 0.368/0.392/0.408/0.018 ms



but this source result is... command line is wrote end of the file..

#include 
#include 
#include 
#include 
#include 
#include 

#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);
        }
    }
}

Bu natija

PING 192.168.30.52 (192.168.30.52) 56(84) bytes of data.
64 bytes from 192.168.30.52: icmp_seq=1 ttl=64 time=0.368 ms
64 bytes from 192.168.30.52: icmp_seq=2 ttl=64 time=0.408 ms
64 bytes from 192.168.30.52: icmp_seq=3 ttl=64 time=0.400 ms
64 bytes from 192.168.30.52: icmp_seq=4 ttl=64 time=0.392 ms
64 bytes from 192.168.30.52: icmp_seq=5 ttl=64 time=0.393 ms

--- 192.168.30.52 ping statistikasi ---
5 ta paket uzatilgan, 5 ta qabul qilingan, 0% paketli yo'qotish, 3996 mln rtt min/avg/maxs/mdev = 0.368/0.392/0.408/0.018 milodiyping -c5 -W1 192.168.30.52

qanday qilib tuzataman ??? /?

3
"DoSystem" ga qo'ng'iroqni amalga oshirishdan oldin fclose qo'shib ko'ring.
qo'shib qo'ydi muallif Jay, manba
"CmdBuf" da yaratgan komandada o'rniga ">" o'rniga harakat qilib ko'ring
qo'shib qo'ydi muallif Jay, manba
Yangi faylni yaratib, avval yangi satrni yozib, so'ng eski faylning qolgan qismini qo'shib qo'ying. Nihoyat, eski sifatida yangi nom bering. Aytganimdek, bu yomon fikr. kundalik fayllar har doim qo'shiladi, qayta yozilmaydi.
qo'shib qo'ydi muallif Olaf, manba
@Olaf, Ankur sizni ham savolni o'qidi? Sarlavha biroz chalg'ituvchi, lekin savolni o'qib, boshqa donolarni aniqlab beradi.
qo'shib qo'ydi muallif Ajay Brahmakshatriya, manba
@Olaf, Ankur sizni ham savolni o'qidi? Sarlavha biroz chalg'ituvchi, lekin savolni o'qib, boshqa donolarni aniqlab beradi.
qo'shib qo'ydi muallif Ajay Brahmakshatriya, manba
vaqtinchalik fayl yaratib, barcha so'nggi ping natijalarini ushbu faylga yozib, faylni oldingi faylga qo'shib, oldingi faylni o'chirib, vaqtinchalik fayl nomini eski fayl nomiga o'zgartiring.
qo'shib qo'ydi muallif Ankur Jyoti Phukan, manba
Men allaqachon "doSystem" dan oldin yopiq bo'lishga harakat qildim. lekin ishlamaslik kerak.!
qo'shib qo'ydi muallif kara, manba
ishlamoqda! rahmat!
qo'shib qo'ydi muallif kara, manba
Nima uchun bu savol to'xtatilgan ?! T_T bu haqida hech narsa emas. ?? .....
qo'shib qo'ydi muallif kara, manba
Nima uchun bu savol to'xtatilgan ?! T_T bu haqida hech narsa emas. ?? .....
qo'shib qo'ydi muallif kara, manba

6 javoblar

in sprintf, > replacing file contents(same as "w" mode). use of >> is same as "a" mode.

#include 
#include 
#include 
#include 
#include 
#include 

#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);
        }    
}
1
qo'shib qo'ydi

in sprintf, > replacing file contents(same as "w" mode). use of >> is same as "a" mode.

#include 
#include 
#include 
#include 
#include 
#include 

#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);
        }    
}
1
qo'shib qo'ydi

Fayllarga chiqish to'liq tamponlangan. Buyruqni ishlatishdan oldin buferni tozalashingiz kerak.

    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);
0
qo'shib qo'ydi
Sizning javobingiz uchun tashakkur !!!!
qo'shib qo'ydi muallif kara, manba

Fayllarga chiqish to'liq tamponlangan. Buyruqni ishlatishdan oldin buferni tozalashingiz kerak.

    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);
0
qo'shib qo'ydi
Sizning javobingiz uchun tashakkur !!!!
qo'shib qo'ydi muallif kara, manba
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);

men bunday deb o'ylayman.

ishlamoqda !

barcha sharhlaringiz tufayli!

0
qo'shib qo'ydi
Kelajakdagi o'quvchilar uchun avvalgi holatlar uchun: faylni yopishni unutganingizdek, birinchi satr tamponlangan, boshida yozgan va dastur oxirida birinchi liniyasi nihoyat tozalangan. Endi siz "w" rejimi bilan oldingi tarkibni o'chirib tashlash uchun faylni yoping va faylning oxirida (>> ) to'g'ri qo'shing.
qo'shib qo'ydi muallif Serge Ballesta, manba
Aloqasiz: void main() o'n yillar mobaynida bekor qilindi ( int main va atrof-muhitga qiymat qaytarish kerak) va har doim siz ochgan oqimlarni yopish uchun yaxshi. ..
qo'shib qo'ydi muallif Serge Ballesta, manba
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);

men bunday deb o'ylayman.

ishlamoqda !

barcha sharhlaringiz tufayli!

0
qo'shib qo'ydi
Kelajakdagi o'quvchilar uchun avvalgi holatlar uchun: faylni yopishni unutganingizdek, birinchi satr tamponlangan, boshida yozgan va dastur oxirida birinchi liniyasi nihoyat tozalangan. Endi siz "w" rejimi bilan oldingi tarkibni o'chirib tashlash uchun faylni yoping va faylning oxirida (>> ) to'g'ri qo'shing.
qo'shib qo'ydi muallif Serge Ballesta, manba
Aloqasiz: void main() o'n yillar mobaynida bekor qilindi ( int main va atrof-muhitga qiymat qaytarish kerak) va har doim siz ochgan oqimlarni yopish uchun yaxshi. ..
qo'shib qo'ydi muallif Serge Ballesta, manba