Column count from data file
Hi all,
I have a dat file called customer.dat with this content:
0 1.1 2 -0.3 4 ...
5 6 7 8 9 ...
10.1 11 12 13 99 ...
...
...
I have no prior idea about number of rows and columns. Below a sample code I have written using fgets. the output gives the number of rows. but I also want to calculate number of columns.
can anybody give me any idea...
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
FILE *dataFile;
char fileName[100];
char customer[100];
int nRow = 0;
printf("Enter the name of the file: ");
fgets(fileName, sizeof(fileName), stdin);
if((dataFile = fopen(fileName, "r")) == NULL) {
printf("Error Opening File.\n");
exit(1);
}
while( fgets(customer, sizeof(customer), dataFile) != NULL ) {
nRow++;
printf("Row %d: %s\n",nRow, customer);
}
fclose(dataFile);
return 0;
}