Q:

Reading from files passed as command line arguements

I am trying to parse a given textfile, but so far, my program does not seem to be reading properly.
#include <stdio.h>

int main(int argc, char *argv[])
{
    FILE *fr; //file pointer
    int buildingFloors = 1;

    printf("sanity check\n");  

    fr = fopen (argv[0], "r");
    fscanf(fr, "%d", &buildingFloors );

    printf("%d\n", buildingFloors);

    fclose(fr);

    return 0;
}

I compile the program and run it on my redhat linux machine with the following command:
./sjf file.text

file.text is a text document with a "4" as the first character. So I would expect my output to be
sanity check
4

However, when I run my program I instead get
sanity check
1

Which implies that fscanf didn't properly read in the first character -- 4. Do I have some syntax error that's preventing the expected code functionality? Am I supposed to scanf for a character, and then convert that to an int somehow?

A:

argv[0] is the name of the program (./sjf in your case), so you're trying to read in your own program's executable.  Use argv[1] instead to get the first real program argument.

A:

One thing which immediatly comes to mind is that the program args include the executable name as the first element
argv[0] is "sjf"
argv[1] is "file.text"
so you should be using 
fr = fopen (argv[1], "r");

Remember when debugging to always try and narrow the problem down, if you know the location of the error the cause often becomes obvious or at least investigatable.
In this case you should check argc >= 2, print out argv[1] to ensure you are trying to open the right file, then also check that the file was opened successfully. 
Finally check the fscanf error codes to see that fscanf was able to read the number.

