I have a little program for testing in which I simply fill an array with some shorts. Then I write this array in a File. And then I print the File.
The code is like this:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFFERLENGTH 10
short bufo[BUFFERLENGTH];
int main(int argc, char **argv){
FILE * ff;
bufo [0]= 1;
bufo [1]= 2;
bufo [2]= 3;
bufo [3]= 4;
bufo [4]= 5;
bufo [5]= 6;
bufo [6]= 7;
bufo [7]= 8;
bufo [8]= 9;
bufo [9]= 10;
short s;
printf("Ho \n");
if ((ff = fopen("prueba1", "ab+")) == NULL){
perror("Abriendo mal");
}
fwrite(bufo, sizeof(short) , 10, ff);
fclose(ff);
printf("FRONTERA \n");
if ((ff = fopen("prueba1", "ab+")) == NULL){
perror("Abriendo mal");
}
while(!feof(ff)){
fread(&s,sizeof(short), 1, ff);
printf("%u \n",s);
}
}
After this I would like to see this output:
1
2
3
4
5
6
7
8
9
10
But instead, I get this :
1
2
3
4
5
6
7
8
9
10
10
Why do I get the last element twice?