I have a (hopefully) quick question. I have been searching around on the internet and through some books but can't seem to find an answer.
I am using text files to store matrices where the elements are separated by tabs (on each line). What I want to do is figure out the number of columns and rows in these files. I figure I could run a routine to count the number of lines to the bottom of the file as well as have another counter that increases by 1 after each fscanf until fscanf reaches \n. This seems pretty slow and cumbersome - is there a faster way?
I am a applied math grad student and am taking a HPC course next semester so I am learning C over break since I don't have much CSCI experience. Any critiques on this code? I know there aren't any things to catch exceptions and check input but other than that stuff what do you guys think? Thanks a lot in advance
Code:
#include <stdio.h>
FILE *f1;
int main (int argc, const char * argv[]) {
char inputname[50];
int matnum1, exit;
exit = 0;
printf("Input filename (must be less than 50 characters including the extension: ");
scanf("%s", inputname);
printf("Opening file %s\n",inputname);
f1 = fopen(inputname, "r");
while (exit != 1) {
fscanf(f1, "%d\t",&matnum1);
printf("%d\n",matnum1);
if (feof(f1)) {
exit = 1;
}
}
return 0;
}