Ha, the tmp conversion is troublesome, indeed. I'm following this route:
Code:
FILE* tmp = tmpfile();
while (fgets(ch, sizeof(ch), fp))
{
printf("Lanzando ping a %s", ch);
sprintf(ping_cmd, "ping %s", ch);
FILE* f = _popen(ping_cmd, "r");
while (fgets(line, sizeof line, f)) {
if (strstr(line, term)) {
fprintf(tmp, "%s\n", ch);
}
}
fclose(f);
}
fclose(tmp);
fclose(fp);
So far, so good, methinks. But then...:
Code:
// tmp = fopen(tmp, "r"); // How do I open the temporary file? Do I close it at all? It just crashes over and over...
while (fgets(ipresp, sizeof(ipresp), tmp))
{
int response1 = velocidad_media(ipresp);
printf("\n Velocidad de respuesta media: %d ms.\n", ipresp, response1);
}
Here I want to recycle that comparison function I used before, to compare between ping results. This time I don't have a predefined number of IP's to compare, but it depends on the actual IP's that responded well during the previous ping procedure.
Code:
int velocidad_media(const char* ip)
{
const char* const Delim = "Media = ";
printf("\n Extrayendo velocidad media de %s", ip);
char cmd[64];
sprintf(cmd, "ping %s", ip);
FILE* p = _popen(cmd, "r");
char line[256];
while (fgets(line, sizeof(line), p))
{
char* s = NULL;
if ((s = strstr(line, Delim)) != NULL)
{
_pclose(p);
return atoi(s + strlen(Delim));
}
}
_pclose(p);
return -1;
}
Except for the tmp conversion, it works almost fine. Only that I want to get the results for the IP's that got a good response and therefore got saved at the "ips_responden.txt" file (tmp file, this time), and this code includes some annoying line break (I think), so I get the extracted info for the IP AND a weird one that isn't at the .txt file. How do I prevent that?