char x[3];
char buff, c;
x[0]='y';
int offset, i;
int fd;
fd = open("test1.txt", O_RDONLY);
if(fd==-1){ printf("Error on fopen."); exit(1); }
offset = lseek(fd, 1, SEEK_END);
printf("Size of file is: %d. \n", offset);
for(i=offset-1; i>=0; i--)
{
c = read(fd, &buff, 1);
printf("The character is: %c. \n", c);
}
close(fd);
运行这个给了我。
Size of file is: 6.
The character is: .
The character is: .
The character is: .
The character is: .
The character is: .
The character is: .
测试文件仅包含单词“TEST”。我希望能够向后打印该单词。
最佳答案
read
从当前位置读取,并将当前位置向前移动读取的量。
还需要进一步寻找。另外 lseek( ..., 1, SEEK_END);
可能应该是 lseek(...,0, SEEK_END);
我对于寻求超越文件。
for( i = ... ) {
lseek(fd, size - i, SEEK_BEGIN );
read(...);
}
关于c - 使用 lseek() 打印出重复的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32554025/