Guys please check the following code.
It prints a file backwards, but has very weird behavior.
Whenever we reach to the beginning of the file,
We go back to the end and print the file over and over again! I don't understand how this could be, fseek() not allow to go back from the beginning.
I'm posting this code even that I've fixed the problem.
I still want to understand how can it be.
Code:
fpRead=fopen("fileseek.c","rb");
if (fpRead==NULL)
{
puts("File not found!");
exit(1);
}
if (fseek(fpRead,-1L,SEEK_END))
{
puts("Error skipping file");
exit(1);
}
while ((fread(&cReadByte,sizeof(char),1,fpRead)) && (ftell(fpRead)>0))
{
//printf("%ld\n",ftell(fpRead));
putchar(cReadByte);
fseek(fpRead,-2,SEEK_CUR);
}
If ftell is enabled the output will be something like this:
12
11
10
9
8
7
6
5
4
3
2
1
12
11
10
.
.
.
Many thanks