I'm writing this function to read in data from a .txt file containing double variables in the form:
1.00 2.00
2.00 3.00
How do i ensure that any empty lines after [2.00,3.00] doesn't get read in and stored?
The code below still reads in and store the value.
Code:
int read_data(double data[][2]){ // return number of set of data
int i=0;
FILE *fp; // File Pointer Declaration
fp = fopen("test.txt","r"); // Open the file for reading
if(fp == NULL) // If file is empty/not present, print error
printf("File Error");
else {
while (!feof(fp)){ // Else carry on till end of file is reached
fscanf(fp,"%lf",&data[i][0]);
fscanf(fp,"%lf",&data[i][1]);
printf("\nRow %d %.2lf %.2lf",i,data[i][0],data[i][1]);
i++;
}
}
fclose(fp); // Close file
return i-1; // Numbers of rows returned