/* hints10.c - This program shows how to read from a text file.
	Suppose this program is compiled and invoked as follows
	    ./a.out hints10.c
	Then it will read each line of hints10.c and print it out
	together with the linenumber.
*/

#include <stdio.h>

// Size of buffer where we read lines
#define BUFFERSIZE 256

int main(int argc, const char *argv[]) {
    if (argc != 2) {
	// Check that there is the right number of command line
	// parameters and if not give error message
	printf("usage: %s filename\n", argv[0]);
	return 1;
    }

    char * filename = argv[1];
    // Open the file
    FILE *fin = fopen(filename, "r");
    if (fin == NULL) {
	// error opening the file. Give up
	printf("Unable to open %s\n", filename);
	return 1;
    }
    int linenumber = 0;
    char buffer[BUFFERSIZE];
    while (fgets(buffer, BUFFERSIZE, fin) != NULL) {
	linenumber++;
	printf("%4d:  %s", linenumber, buffer);
    }
    // Close the file you had opened (be polite)
    fclose(fin);
    return 0;
}
