problem with a very simple addition of digits

The code below creates a file, it stores in it all the numbers from 1 to 100. Closes the file. Then, it opens the file, reads all the numbers one after the other and attempts to find the sum of them. The sum should be 1+2+3+.....+98+99+100 = 5050. Unfortunately, i cannot understand why it counts TWICE the last number. This is what it does:
1+2+3+.....+98+99+100+100 = 5150. I imported and used functions only from the c language library.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <conio.h>

int main(void)
{
    int i=1,cnt=0,ar;
    float sum=0.0,avg;
    FILE *fp;
    fp = fopen("numbers.txt","w"); //file is stored in the same folder as the c source code.
    if(fp == NULL)
        printf("not found\n");
    else
        while(!feof(fp))
        {
            fprintf(fp,"%d ",i++);
            if(i==101)
                break;

        }
    fclose(fp);
    fp = fopen("numbers.txt","r");
    if(fp == NULL)
        printf("not found\n");
    else
        while(!feof(fp))
            {
                fscanf(fp,"%d",&ar);
                printf("%d ",ar);
                sum+=ar;
            }
    printf("\nsum = %.1f\n",sum);

    return 0;
}
  
Last edited on
The problem is when you read the value with fscanf(), you need to check the return code.
Hi,

Don't loop on eof. See if that fixes anything. Not at all sure why you do it when writing to the file - never seen that before.


http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong


SO wrote:
The answer

while(!eof) is wrong because it tests for something that is irrelevant and fails to test for something that you need to know. The result is that you are erroneously executing code that assumes that it is accessing data that was read successfully, when in fact this never happened.


Also on line 19 you have a limit of 101.
Last edited on
thank you. The first 23 lines are fine.They will store numbers 1- 100 in a file. I tetsed it.
The problem is with the lines from line 28 and below. I understand that i need to check the return code BEFORE i make an addition. I am stuck because i cannot come up with that very simple code line to check the return code so as to prevent the extra unwanted calculation.

I know that i open the file in text mode and i am using a function feof() which is more ideal for binary mode, but it seems to be working,i cannot use EOF in the while statement. Maybe i run out of ideas how to write some proper code lines :(
The first 23 lines are fine.They will store numbers 1- 100 in a file. I tetsed it.


That doesn't mean it is right :+) Just use fprintf, no EOF. It returns a value as well, so you can check that after every write operation, and handle any rare errors.

The answers to your other questions are in the link I posted.

Use the return value from fscanf to see if it worked or not. If it does, then proceed. Otherwise, check for EOF and break or print an error message (or otherwise handle) if no EOF.

The big thing to remember here: EOF is set after a failed read operation.
Topic archived. No new replies allowed.