Thread: Extracting and comparing DNS servers from a .txt file

  1. #16
    Registered User
    Join Date
    May 2020
    Posts
    39
    Double posted and asked some further nonsense, haha. Sorry for the hassle.
    Now, after having spent my time modularizing the whole program, my main looks like this:

    Code:
    int main()
    {
    
        FILE* fp;
        {
            printf("\n    Escribe la ruta del documento deseado: ");
            scanf("%s", x);
            printf("\n");
            fp = fopen(x, "r");
            if (fp == NULL)
            {
                printf("\n    El archivo no se ha podido encontrar\n\n");
                exit(0);
            }
            while (fgets(ch, sizeof(ch), fp))
            {
                printf("%s", ch);
            }
            printf("\n");
            fclose(fp);
    
            leerAdaptadores();
            leerIpsValidas();
            comparativaIps();
            extraerDNS();
            comparativaDNS();
            }
    
        }
    So far, so good.
    The DNS extraction works perfect. I've that "dns_responden.txt" filled with the data I need, i.e. DNS lined one after the other with line breaks among them, just like my "ip_responden.txt" file I needed for "comparativaIps()":

    Code:
    void comparativaIps()
    {
        int best_response = 0;
        char ipresp[100], best_ip[sizeof(ipresp)] = "";
        int best_valid = 0;
        FILE* responses = fopen("ips_responden.txt", "r");
        if (responses != NULL)
        {
            while (fgets(ipresp, sizeof(ipresp), responses))
            {
                int response1 = velocidad_media(ipresp);
                if (!best_valid || response1 < best_response) {
                    best_response = response1;
                    strcpy(best_ip, ipresp);
                    best_valid = 1;
                }
            }
            printf("\n    IP seleccionada: %s", best_ip);
            printf("\n    Velocidad de respuesta media: %d ms.\n", best_response);
            fclose(responses);
        }
    }
    As for the "comparativaDNS()" block, it's just the same as "comparativaIps()" and still unable to print what I expect to do...:

    Code:
    void comparativaDNS()
    {
        int best_response_time = 0;
        char ip_str[100], best_dns[sizeof(ip_str)] = "";
        int best_valid_dns = 0;
        FILE* responsesdns = fopen("dns_responden.txt", "r");
        if (responsesdns != NULL)
        {
            while (fgets(ip_str, sizeof(ip_str), responsesdns))
            {
                int dns1 = velocidad_media(ip_str);
                if (!best_valid_dns || dns1 < best_response_time) {
                    best_response_time = dns1;
                    strcpy(best_dns, ip_str);
                    best_valid_dns = 1;
                }
            }
            printf("\n\n    Velocidad de respuesta media: %d ms.\n", best_response_time);
            printf("\n    DNS seleccionada: %s", best_dns);
        }
    }
    Last edited by JSteel; 05-22-2020 at 05:18 AM.

  2. #17
    Registered User
    Join Date
    May 2020
    Posts
    39
    Got it on my own! My extraction of DNS servers lacked a fclose at the end of it. I was close to insanity...

  3. #18
    Registered User
    Join Date
    May 2020
    Posts
    39
    Now, and this is the final part of my project: once I've got the best response for both my IP's and DNS servers, I must compare them and, if IP shows a faster time of response, replace the DNS server of such network interface that lost the comparison.

    Once modularized, I need to call the variables within different modules to compare them:

    Code:
            leerIpsValidas();
            printf("\n\n");
            comparativaIps();
            extraerDNS();
            printf("\n");
            comparativaDNS();
            if (best_response_time > best_response)
    blah blah...
    best_response_time belongs to the comparativaDNS module and best_response to comparativaIps. Not sure how to invoke them back from main.

    And one final thing, which troubles me the most. I'd selected the network interface before, in the "leerAdaptadores" module, with a number ("adaptador" variable) that got me a system command like "netsh interface ipv4 show dnsservers adaptador" for the DNS server extraction. The system call to replace a given DNS server would be something like "netsh interface ipv4 set dns name=adaptador static best_response", but instead of a number, a full string of text with the name of such interface. How can I omit this approach, so I'm able to omit special characters such as Conexión?

  4. #19
    Registered User
    Join Date
    May 2020
    Posts
    39
    Ha, finally I've gone through it - seems like numbers also worked with that, and the value of my variable was used as well.
    I'm finally done with this project. Thanks A LOT anyone who provided help - much appreciated!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help extracting JPG from .WAV file
    By MattJunior in forum C++ Programming
    Replies: 15
    Last Post: 07-20-2017, 06:16 AM
  2. Extracting Data from txt File
    By BonaviaFx in forum C Programming
    Replies: 1
    Last Post: 01-13-2015, 05:10 AM
  3. Extracting and comparing numbers
    By .C-Man. in forum C++ Programming
    Replies: 47
    Last Post: 03-01-2011, 08:45 AM
  4. extracting file names
    By cathal in forum C Programming
    Replies: 1
    Last Post: 08-19-2004, 08:57 AM

Tags for this Thread