shouldn't you check that it ends with ".csv" if that is the file format?Code:void the_file_name(char *filename){ int length; do { printf("Enter a filename with .xls at the end please. "); fflush(stdout); scanf("%s", filename); length = strlen(filename); } while(filename[len-1] != 's' || filename[length-2] != 'l' || filename[len-3] != 'x' || filename[length-4] != '.'); //FIINISHED }
And you could use strcmp(&filename[len-4], ".csv") != 0 to check if it's a mis-match. Of course, I personally prefer that applications just take a filename as I type it - or at least, if it's got an extension, that it's not adding another one - say I saved my file as "list.txt", but it's really a .csv file, why should your application NOT work with that?
Not sure if this is just a place-holder, but "%a" is not a standard format specifier:
It is generally not a great idea to read the file to find out how many lines it has, then allocate memory, and then read the file again. If you fell you MUST do this, why not use fgets() with a long buffer - at least then you will be reading reasonably efficiently, where as fscanf() is a pretty processor intensive way to read a file. [No, you probably won't be able to measure the difference on a file less than a few megabytes, but if no one tells you until you are wondering why your mega-huge file takes ages to read, how are you going to know the right/wrong way to do it?]Code:fscanf(fp, "%a", &a)
There are two ways I'd prefer to do it:
Method A:
* Have a linked list of projects (or a fixed array of "too large", e.g. 300 projects as a maximum).
* Read in a line of the file, parse the name and score. Check the project list. If it's in the list, check if the score is higher: Replace, otherwise don't do anything. If it's not in the list, it's obviously a new highest score, so add it to the list.
Method B:
* Start with mallocing a "person array" decent number of person slots, say 100. Set a size variable to 100.
* While not end of file:
* Read a line.
* check if there is space in "person array", then put it in the person array. If person array is full, use realloc to double it's size (set size variable to 2 x size), and insert it in one of the new free slots.
A note on style, you are inconistent in your file naming, choose one or the other:
Code:readFileIntoArray read_the_file_to_array
--
Mats