Just implemented the strcmp bit - works great, thanks!
I'm toying around a bit with the later piece, will keep you posted.
What troubles me is to be able to print error if the IP doesn't show connectivity, and therefore be able to omit the comparison bit printing some kind of extra error à la "the program couldn't compare between both DNS". That's why I tried, to no avail, with something in the lines of:
Code:
if (a = true) printf("\n\n La velocidad media de DNS 1 es %s ms.", target);
if (a = false) printf("\n DNS 1 muestra error de conectividad.");
I should be able to print it for both results, i.e. duplicating the code with a different variable (instead of a, let's say b) and only do the comparison bit if both a and b are true, else print me an error. It's an option, but I don't know how to proceed, and the program crashes everytime...
Edit: Trying implementing the second one. Looks like a mess the way I did, haha..., maybe I'm able to compare both results without converting to int? When both IP's responded well, I could do the math and compare both as strings...
So that's what I have so far, which crashes by the end of the program if one of the IP's doesn't show connectivity:
Code:
void compararDns() {
FILE* fp;
char dns1[16], dns2[16], str[50], line[50];
const char* PATTERN1 = "Media = "; // Declararemos las variables mediante las cuales extraemos la cadena de caracteres relativa a las velocidades en nuestro diagnóstico ping.
const char* PATTERN2 = "ms";
char* target = NULL;
char* target2 = NULL;
char* start, * end;
int ns = 0, nt = 0;
int val1, val2;
bool a, b;
// Bloque a través del cual el usuario analiza las IP's correspondientes para analizar.
printf("\n Escribe la primera IP: ");
scanf("%s", dns1);
printf(" Escribe la segunda IP: ");
scanf("%s", dns2);
// Bloque mediante el cual el ping es lanzado y se nos informa del proceso en curso.
printf("\n\n Lanzando ping sobre %s...", dns1);
strcpy(str, "ping ");
strcat(str, dns1);
strcat(str, " > dns1.txt");
system(str);
printf("\n Lanzando ping sobre %s...", dns2);
strcpy(str, "ping ");
strcat(str, dns2);
strcat(str, " > dns2.txt");
system(str);
// Proceso interno mediante el cual extraemos la información del archivo redireccionado mediante los delimitadores declarados.
fp = fopen("dns1.txt", "r");
while (fgets(line, sizeof(line), fp))
if (start = strstr(line, PATTERN1))
{
start += strlen(PATTERN1);
if (end = strstr(start, PATTERN2))
{
target = (char*)malloc(end - start + 1);
memcpy(target, start, end - start);
target[end - start] = '\0';
}
}
// Printamos el resultado de la velocidad de DNS 1.
if (target) printf("\n\n La velocidad media de DNS 1 es %s ms.", target);
else printf("\n DNS 1 muestra error de conectividad.");
// Proceso interno mediante el cual extraemos la información del archivo redireccionado mediante los delimitadores declarados.
fp = fopen("dns2.txt", "r");
while (fgets(line, sizeof(line), fp))
if (start = strstr(line, PATTERN1))
{
start += strlen(PATTERN1);
if (end = strstr(start, PATTERN2))
{
target2 = (char*)malloc(end - start + 1);
memcpy(target2, start, end - start);
target2[end - start] = '\0';
}
}
// Printamos el resultado de la velocidad de DNS 2.
if (target2) printf("\n La velocidad media de DNS 2 es %s ms.", target2);
else printf("\n DNS 2 muestra error de conectividad.");
// Comparamos ambas velocidades y printamos la superior.
int result = strcmp(target, target2);
if (result < 0) printf("\n DNS1 ofrece mejor respuesta.\n");
else if (result > 0) printf("\n DNS2 ofrece mejor respuesta.\n");
else printf("\n La respuesta ha sido la misma.\n");
// Liberamos las variables que contienen los resultados de las velocidades medias almacenadas.
free(target);
free(target2);