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
#------------   -----   -------    ---------         13   1177136          1   4533883    Google         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>

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

    fr=fopen(filename, "r");
        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')
            sscanf(buf, "%s.%s %d %d", &domain, &dottype, &alexa, &rank); 

            if(alexa<min_alexa){ /*finding and storing best alexa*/
                strcpy(min_alexaname, domain);
                strcpy(min_alexadot, dottype);
            if(rank>max_rank){ /*finding and storing best ranking*/
                strcpy(max_rankname, domain);
                strcpy(max_rankdot, dottype);
            }else if(strcmp(dottype,"net")==0){
            }else if(strcmp(dottype,"org")==0){
    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


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

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.