Search code examples
cvisual-studio-codeerror-checking

Trouble adding correct error checks to code


For my assignment I was given a program to add error checks to, I was add the first two quickly (any comment with an X after it I believe I've solved) However I believe the problem for this error check in line 60 is that fscanf willingly reads in chars to something that takes integers, so I need to add something that prints out an error and stops the program if a char is attempted to be read in. I am also not sure what to error check for in create_graph and read_edge yet. The files to be read into this program are of a format like this:

4 5
1 2 0.2
2 3 0.3
3 4 -3.7
1 4 0.2
3 1 0.4

My most recent attempt has been:

   if (scanf("%d", &n) == 0 || scanf("%d", &m) == 0){
    printf("Error: Expected an Integer");

    return 0;
}

Current code:

to try and scan the input to make sure they're integers.

// missing error check (you may need to modify the function's return
// value and/or parameters)
edge read_edge(FILE* file) {
    edge e;
    fscanf(file, "%d %d %f", &e.source, &e.target, &e.weight);
    return e;
}

graph create_graph(int n, int m) {
    graph g = {
        .n = n,
        .m = m,
        .vertices = calloc(n, sizeof(vertex)),
        .edges = calloc(m, sizeof(edge)),
    };

    for(int i = 0; i < n; i++) {
        g.vertices[i] = i + 1;
    }

    return g;
}

int main(int argc, char* argv[]) {
    // missing error check -- related to argc/argv X
    if (argv[2] != '\0')
    {
        printf("Wrong number of arguments.\n");
        return 0;
    }
    // missing error check (errno) X
    FILE* file = fopen(argv[1], "r");

    if (file == NULL) {
        printf("Unable to open file: %s\n", strerror(errno));
        return 0;
    }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    int n, m;
    // missing error check
    fscanf(file, "%d %d", &n, &m);

    if (scanf("%d", &n) == 0 || scanf("%d", &m) == 0){
        printf("Error: Expected an Integer");

        return 0;
    }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    
    graph g = create_graph(n, m);

    for (int i = 0; i < m; i++) {
        // missing error check (after you fix read_edge)
        g.edges[i] = read_edge(file);
    }

    printf("%d %d\n", g.n, g.m);

    return 0;
}

As it is now the program just crashes when trying to read in a file.


Solution

  • How to error check:

    fscanf(file, "%d %d", &n, &m);
    

    Suggest:

    if( fscanf(file, "%d %d", &n, &m) != 2 )
    {
        fprintf( "fscanf of first line from the input file failed\n );
        exit( EXIT_FAILURE );
    }
    
    // implied else, fscanf successful
    

    Note: the scanf() family of functions returns the number of successful input format conversions (or EOF)

    regarding:

    if (argv[2] != '\0')
    {
        printf("Wrong number of arguments.\n");
        return 0;
    }
    

    When discussing a command line parameter problem, it is best to display (on stderr) a USAGE statement, similar to:

    if( argc != 2 )
    {
        fprintf( stderr, "USAGE: %s <inputFileName>\n", argv[0] );
        exit( EXIT_FAILURE );
    }
    
    // implied else, correct number of command line parameters
    

    Note: argv[0] is always the name of the executable