1

I want to save some information in a file text, I wrote this program:

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

Results:

buffer is: 127.0.1.1
command is : ping -c 1 -W 1 127.0.1.1

the system command returns this:

    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

But when I run : cat /tmp/ping_result.I got an empty file

Slim Hmidi
  • 25
  • 3
  • 1
    Add some prints in between. Or use a debugger. – Eugene Sh. Oct 11 '16 at 13:51
  • Check the return values of your function calls, especially your calls to `system()` in this case. – John Bollinger Oct 11 '16 at 13:54
  • @JohnBollinger: I edited the post – Slim Hmidi Oct 11 '16 at 13:59
  • 1
    "> /tmp/ping_result" seems to be missing in your command, I'm a bit rusty with filehandles, but has your buffer a string termination? – Kami Kaze Oct 11 '16 at 14:02
  • @KamiKaze I don't understand you – Slim Hmidi Oct 11 '16 at 14:05
  • 1
    your `printf` of command is missing the last part where you write to the file. So it seems it is not there, this part comes after the buffer in your `snprintf`you get your `buffer` from a file. Have you made sure your string in `buffer` gets terminated (by a \0) or is it just 20 chars. – Kami Kaze Oct 11 '16 at 14:07
  • 2
    I think there is a newline in `buffer` that messes up your `command`. – Klas Lindbäck Oct 11 '16 at 14:13
  • @KlasLindbäck is certainly correct. Try `printf("buffer is: <%s>\n",buffer);` – chux - Reinstate Monica Oct 11 '16 at 14:15
  • it is just 20 chars – Slim Hmidi Oct 11 '16 at 14:21
  • @chux there is a newline in the buffer how to fix that – Slim Hmidi Oct 11 '16 at 14:24
  • when compiling, always enable all the warnings, then when the compiler outputs warning messages, fix the code. The posted code results in 3 warnings 1) unused variable: `command1[]` 2) unused parameter `argc` 3) unused parameter `argv` – user3629249 Oct 12 '16 at 19:48
  • when calling system functions that can return an error indication, always check that returned value to assure the operation was successful. In the posted code, that includes: `system()`, `fopen()`, `fread()` – user3629249 Oct 12 '16 at 19:50
  • in C, when refering to an array name, that name degrades to the address of the first byte of the array, so this line: `memset(&buffer,0,sizeof(buffer));` should be: `memset( buffer, 0, sizeof(buffer) );` Note: appropriate horizontal spacing makes the code much easier to read/understand – user3629249 Oct 12 '16 at 19:55

2 Answers2

0

Problem: Code is reading '\n' into buffer[] yet trying to that as part of the command. Need to trim the buffer. *** below

 // 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")] = '\0';  // ***

   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);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

the posted code has a couple of problems

1) outputs results of ping to stdout rather than to /tmp/ping_result

2) fails to removed trailing newline from the buffer[] array

The following code

1) cleans up the indenting

2) corrects the problems in the code

3) handles possible failure of call to fopen()

4) eliminates unneeded final statement: 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 $2}' | cut -d'.' -f1-3 | awk '{print $1\".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] = '\0'; // 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);
}

the resulting output, on my computer, is in file: /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
user3629249
  • 16,402
  • 1
  • 16
  • 17