The problem is in the test of while
:
while(!feof(arq)){
That actually tests whether any reading has been made beyond the end, like the documentation indicates
This Indicator is generally set by a Previous Operation on the stream that attempted to read at or Past the end-of-file.
Notice that stream’s Internal position Indicator may point to the end-of-file for the next Operation, but still, the end-of-file Indicator may not be set until an Operation Attempts to read at that point.
Translating
This indicator is activated by a previous operation in the stream you tried to read beyond the end of the file.
Note that the internal position of the stream may be pointing to the end of the file for the next operation, but still, the end of file indicator will not be activated until a read is made.
This makes the last reading read the 3
which leaves the file at the end, but then tries to read again and only then activates the end of file indicator, showing then twice the 3
.
To correct change your while
for:
while(fscanf(arq, "%d", &conta) == 1) {
printf("%d\n", conta);
}
So check for the return of fscanf
if managed to read a number and only in this case makes the impression on the screen.
with this change in the loop works normally:char c while((c = getc(Arq) ) != EOF). But still do not understand the pq of the first print twice the last line :/
– Caio Teixeira
Caio, avoid using the c++ tag in questions about C. The two languages are quite different.
– Pablo Almeida