#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <stdint.h>
int main()
{
int file;
off_t offset;
if((file=open("testfile.txt",O_RDONLY)) < -1)
return 1;
char buffer[19];
if(read(file,buffer,19) != 19) return 1;
fprintf(stdout,"%s
",buffer);
if(offset = lseek(file,-19,SEEK_END) < 0) return 1;
fprintf(stdout,"%jd
",(intmax_t)offset);
if(read(file,buffer,19) != 19) return 1;
fprintf(stdout,"%s
",buffer);
return 0;
}
The Output is as follows:
This is a test file??
0
his is a test file
??
testfile.txt :
This is a test file Testing how SEEK_END works This is a test file
I have tried different formatting for offset, such as %ld,%d, but the output is still the same.
Can't figure out why garbage appears at the end of first line and last line. Please help.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…