Im trying to read in some data from a file (ultimately into a structure, but not important for now) and to ensure that this file has equal amount of data on each line. Each line can have words or numbers though so I've converted one line of a file into one big string. I then try and split this string into tokens using strtok and commas (which separate the data) as delimiters. But how can i count the amount of tokens that exist between commas. I've tried to count the amount of commas on each line but for some reason it is not behaving as it i expect it to. Each line in the file has 5 pieces of data, all split by commas, so there should be 4 commas on each line.
while (fgets(string, sizeof(string), f)) {
input = fgetc(f);
if(input == ','){
i++;
}
else if (input == ' '){
printf("Error");
exit(0);
}
}
if(i % 4 != 0){
printf("Error");
exit(0);
}
Here i am trying to count the amount of commas on every line (and if theres a space on the file line, it should show an error, as I ONLY want commas dividing the data). Finally after fgets stops reading, I want to see if the "i" variable is a multiple of 4. Im sure there is a more efficient and user friendly way to do this though but I cant think of one.
Quick question as well: does the fgetc run through every character on the line before the rest of the commands continue, or as soon as a comma is encountered, my program will move on the next loop?
Thank you!