Hi I wrote a function that inputs a string from a file and then prints out the answer, now my loop is executing more then the file has lines: this is my code :
void CalculateAll()
{
char str[SIZE];
int ans;
FILE* p = fopen("Expressions.txt", "r");
if (p == NULL)
{
printf("FAIL");
return;
}
while (!feof(p))
{
int i = 0;
fgets(str, SIZE - 1, p);
while (str[i] != '\n')i++;
str[i] = 0;
ans = CalculateExpression(str);
printf("%d\n", ans);
}
}
the file has
121 + 34 =
562 + 45 =
100 / 10 =
the output should be
155
607
10
but instead I get :
155
607
10
10
and my loop is running again even tho I reached the end of the file, does anyone have an idea? EDIT: its not the same question..
When I changed my code to this my first line is gone :
void CalculateAll()
{
char str[SIZE];
int ans;
FILE* p = fopen("Expressions.txt", "r");
if (p == NULL)
{
printf("FAIL");
return;
}
while ((fgets(str, SIZE - 1, p)))
{
int i = 0;
fgets(str, SIZE - 1, p);
while (str[i] != '\n')i++;
str[i] = 0;
ans = CalculateExpression(str);
printf("%d\n", ans);
}
}