Hello,
I have a problem with finding the end of the text file. I created a piece of code to scan my txt file for the commands, but the problem is that for some reason one of the commands gets implemented twice. I checked everything and the problem is in this piece. It's like feof finds the end of the file not correctly. Could someone give me a couple of new ideas how to make a loop and to read everything in the file correctly? Thanks
Code:
int FileAccess( )
{
int ret;
FILE *acc_file = NULL;
acc_file = fopen(notename, "r");
if (acc_file == NULL) //Checking if file exists
{
printf("Error opening file! Please check whether the file exists...\n\n");
return 1;
}
else
{
while (!feof(acc_file))
{
fscanf(acc_file,"%s",str);
if (strcmp(str,"FL")==0)
{
fscanf(acc_file,"%d %d %d", &flood_colour.red, &flood_colour.green, &flood_colour.blue);
FloodImage(flood_colour);
}
else if (strcmp(str,"LI")==0)
{
fscanf(acc_file,"%d %d %d %d", &x_start, &y_start, &x_finish, &y_finish);
fscanf(acc_file,"%d %d %d", &line_colour.red, &line_colour.green, &line_colour.blue);
DrawLine(x_start, y_start, x_finish, y_finish, line_colour);
}
else if (strcmp(str,"CH")==0)
{
CheckpointImage( );
}
else if (strcmp(str,"RE")==0)
{
RefreshImage( );
}
else if (strcmp(str,"EX")==0)
{
break;
}
}
fclose(acc_file);
printf("File was successfully read...\n");
printf("All the tasks were executed...\n\n");
return 0;
}
}