0

I'm having some trouble coding in C an iteration, that's to be taken from a .txt file where there are several IP's listed one above the other in several lines. What I need to do is to print the whole content of the file and immediately after, ping each one of the IP's automatically, one after the other. I've been struggling for some hours and I'm unable to get it - I have no idea for the iteration, and so far I've only been able to print the actual file but it just pings the 2nd line instead of the first one.


    FILE* fp;
    char ch[500], x[225], str[80];

    printf("\n    Escribe la ruta del documento deseado\n\n");
    scanf("%s", x);

    fp = fopen(x, "r");

    if (fp == NULL)
    {
        printf("El archivo no se ha podido encontrar.");
        exit(0);
    }

    while (fgets(ch, 100, fp)) {
        printf("%s", ch);
    }

            fgets(ch, 100, fp);
            strcpy(str, "ping ");
            strcat(str, ch);
            system(str);


}

Any help? Thanks in advance.

  • With C first you need to use `while` to know the numbers of lines that contains each IP in the file, then use `malloc` to use pointers and creates an array and finally iterate again the file to populates the array and then use it. For this case is more easy to use Perl, Bash, Python or Powershell than C. Which operating system are you using, *nix or Windows? – Ramiro Encinas May 02 '20 at 18:04

1 Answers1

0

Well, just got it solved miraculously by adding a "while (!feof(fp))". Now I'm just wondering how could I determine, via C, if a ping gives a true or false response. I bet some "if... else" might do, but I cannot for the life of me figure out how could it work in a ping environment...