I am reading 15 numbers from a text file, with each number in a new line:
1 2 3 4 5 10 12 13 14 15 21 22 23 24 26
As you can see from the code I need the numbers to be validated so they are less than 26, otherwise terminate the program.
At the moment I am validating only after inserting it to the array (numArray). Is there a cleaner way of doing it (to validate before inserting to the array)?
The problem is, I can't seem to get the actual line in the text file that's being read. That's why I've validated it using the loop index on the array (int x = numArray[i];).
Any help is appreciated, I'm quite new to C programming. Thanks.
FILE *myFile = fopen(dataset.txt, "r");
int numArray[15];
if (myFile != NULL) {
for (int i = 0; i < sizeof(numArray); i++)
{
//insert int to array
fscanf(myFile, "%d", &numArray[i]);
//Validate number
int x = numArray[i];
if (x > 25) {
printf("Invalid number found, closing application...");
exit(0);
}
}
//close file
fclose(myFile);
}
else {
//Error opening file
printf("File cannot be opened!");
}