So I am working on a simple assignment for my C class that reads a formatted file and prints information about the file based on the format. The file format looks like this:

#Domain         Alexa      Rank    Site Name
#------------   -----   -------    ---------
amazon.com         13   1177136    Amazon.com
google.com          1   4533883    Google
youtube.com         3   3637788    YouTube

plus a lot more lines.

I am almost done, but variable scope (I think) is causing me some problems with my program giving the correct output. Here is my code, minus a bunch of stuff that doesn't apply to my problem (pardon me if the readability is questionable. Its working code and I haven't cleaned it up in a while, so its kind of a mess):

#include <stdio.h>
#include<string.h>

char *filename="web-data";
FILE *fr;
int main(){

    fr=fopen(filename, "r");
    if(fr==NULL)
        fprintf(stderr, "can't open %s\n", filename);


    int i=0;
    char domain[15];
    char dottype[4];
    int com;
    int net;
    int org;
    int other;
    int alexa;
    long int rank;
    char buf[200];
    long int max_rank=0;
    char max_rankname[15];
    char max_rankdot[4];
    int min_alexa=1000;
    char min_alexaname[15];
    char min_alexadot[4];
    fr=fopen(filename, "r");

    while(fgets(buf,200,fr)!=NULL){ /*pulls one line from the file at a time*/
        if(buf[0]==' ' || buf[0]=='#' || buf[0]=='\n')
            continue;
        else{    
            sscanf(buf, "%s.%s %d %d", &domain, &dottype, &alexa, &rank); 

            if(alexa<min_alexa){ /*finding and storing best alexa*/
                min_alexa=alexa;
                strcpy(min_alexaname, domain);
                strcpy(min_alexadot, dottype);
            }else
                continue;
            if(rank>max_rank){ /*finding and storing best ranking*/
                max_rank=rank;
                strcpy(max_rankname, domain);
                strcpy(max_rankdot, dottype);
            }else
                continue;
            if(strcmp(dottype,"com")==0){
                com++;
            }else if(strcmp(dottype,"net")==0){
                net++; 
            }else if(strcmp(dottype,"org")==0){
                org++;
            }else
                other++;
            i++;
        }
    }
    printf("Best Rank: %ld %s.%s\n", max_rank, max_rankname, min_alexadot);
    printf("Best Alexa: %d %s.%s\n", min_alexa, min_alexaname, min_alexadot);
    printf(".com: %d\n", com);
    printf(".net: %d\n", net);
    printf(".org: %d\n", org);
    printf("Other: %d\n", other);
}

This is what my output ends up looking like, with slight variations depending on what I tweak to try and fix it:
Best Rank: 0
Best Alexa: 1000 X&.
.com: 0
.net: 32767
.org: 1099366320
other: 0

Thanks

edit: fixed some real stupid mistakes I had in my code. Still doesnt run correctly, but its better.

Edited 2 Years Ago by izzytoots: I forgot some stuff

The first error that I immediately noticed is you're not initializing your variables to 0. This means that they'll have some random value, and incrementing that value (on top of being undefined behavior) will accomplish nothing meaningful.

Yeah. I figured that out last night shortly after posting. I'm still having the same output problems though. When using gdb to debug, most of the values I have running through the conditionals don't get new values when the loop runs, even though I have the variable initialized on the outside of the loop.

This question has already been answered. Start a new discussion instead.