在这种情况下文件的 lseek 发生了什么事情

问题描述 投票:0回答:2

我尝试多次打开同一个文件进行阅读,但遇到了意外的结果。内容似乎过度重复,而不是获得期望的结果。问题表现如下:

主要功能:

int main(void)
{
    int fd = open("file.txt", O_RDONLY);
    int fd1 = open("file.txt", O_RDONLY);
    int fd2 = open("file.txt", O_RDONLY);
    int fd3 = open("file.txt", O_RDONLY);
    int fd4 = open("file.txt", O_RDONLY);
    int fd5 = open("file.txt", O_RDONLY);
    int fd6 = open("file.txt", O_RDONLY);
    printf("%s", get_next_line(fd));
    printf("%s", get_next_line(fd1));
    printf("%s", get_next_line(fd2));
    printf("%s", get_next_line(fd3));
    printf("%s", get_next_line(fd4));
    printf("%s", get_next_line(fd5));
    printf("%s", get_next_line(fd6));
    printf("%s", get_next_line(fd1));
    printf("%s", get_next_line(fd2));
    printf("%s", get_next_line(fd3));
    printf("%s", get_next_line(fd4));
    printf("%s", get_next_line(fd5));
    printf("%s", get_next_line(fd6));
    printf("%s", get_next_line(fd));
    printf("%s", get_next_line(fd1));
    printf("%s", get_next_line(fd2));
    printf("%s", get_next_line(fd3));
    printf("%s", get_next_line(fd4));
    printf("%s", get_next_line(fd5));
    printf("%s", get_next_line(fd6));
    printf("%s", get_next_line(fd1));
    printf("%s", get_next_line(fd2));
    printf("%s", get_next_line(fd3));
    printf("%s", get_next_line(fd4));
    printf("%s", get_next_line(fd5));
    printf("%s", get_next_line(fd6));
    printf("%s", get_next_line(fd));
    printf("%s", get_next_line(fd1));
    printf("%s", get_next_line(fd2));
    printf("%s", get_next_line(fd3));
    printf("%s", get_next_line(fd4));
    printf("%s", get_next_line(fd5));
    printf("%s", get_next_line(fd6));
    printf("%s", get_next_line(fd1));
    printf("%s", get_next_line(fd2));
    printf("%s", get_next_line(fd3));
    printf("%s", get_next_line(fd4));
    printf("%s", get_next_line(fd5));
    printf("%s", get_next_line(fd6));
    printf("%s", get_next_line(fd));
    printf("%s", get_next_line(fd1));
    printf("%s", get_next_line(fd2));
    printf("%s", get_next_line(fd3));
    printf("%s", get_next_line(fd4));
    printf("%s", get_next_line(fd5));
    printf("%s", get_next_line(fd6));
    printf("%s", get_next_line(fd1));
    printf("%s", get_next_line(fd2));
    printf("%s", get_next_line(fd3));
    printf("%s", get_next_line(fd4));
    printf("%s", get_next_line(fd5));
    printf("%s", get_next_line(fd6));
}

输出:

In the vast realm of programming, languages dance with logic, 
In the vast realm of programming, languages dance with logic, 
In the vast realm of programming, languages dance with logic, 
In the vast realm of programming, languages dance with logic, 
In the vast realm of programming, languages dance with logic, 
In the vast realm of programming, languages dance with logic, 
In the vast realm of programming, languages dance with logic, 
weaving intricate spells of code. Brackets embrace functions, 
weaving intricate spells of code. Brackets embrace functions, 
weaving intricate spells of code. Brackets embrace functions, 
weaving intricate spells of code. Brackets embrace functions, 
weaving intricate spells of code. Brackets embrace functions, 
weaving intricate spells of code. Brackets embrace functions, 
weaving intricate spells of code. Brackets embrace functions, 
loops iterate like rhythmic beats, and variables hold the secrets of data. 
Bugs lurloops iterate like rhythmic beats, and variables hold the secrets of data. 
Bugs lurloops iterate like rhythmic beats, and variables hold the secrets of data. 
Bugs lurloops iterate like rhythmic beats, and variables hold the secrets of data. 
Bugs lurloops iterate like rhythmic beats, and variables hold the secrets of data. 
Bugs lurloops iterate like rhythmic beats, and variables hold the secrets of data. 
Bugs lurk in the shadows, challenging the coder's resolve, yet debugging becomes a quest for perfection. 
Algoritk in the shadows, challenging the coder's resolve, yet debugging becomes a quest for perfection. 
Algoritk in the shadows, challenging the coder's resolve, yet debugging becomes a quest for perfection. 
Algoritk in the shadows, challenging the coder's resolve, yet debugging becomes a quest for perfection. 
Algoritk in the shadows, challenging the coder's resolve, yet debugging becomes a quest for perfection. 
Algoritk in the shadows, challenging the coder's resolve, yet debugging becomes a quest for perfection. 
Algoritloops iterate like rhythmic beats, and variables hold the secrets of data. 
Bugs lurhms are the architects, designing the blueprint of digital landscapes. 
Syntax, the hms are the architects, designing the blueprint of digital landscapes. 
Syntax, the hms are the architects, designing the blueprint of digital landscapes. 
Syntax, the hms are the architects, designing the blueprint of digital landscapes. 
Syntax, the hms are the architects, designing the blueprint of digital landscapes. 
Syntax, the hms are the architects, designing the blueprint of digital landscapes. 
Syntax, the language's grammar, guides programmers through the labyrinth of possibilities. language's grammar, guides programmers through the labyrinth of possibilities. language's grammar, guides programmers through the labyrinth of possibilities. language's grammar, guides programmers through the labyrinth of possibilities. language's grammar, guides programmers through the labyrinth of possibilities. language's grammar, guides programmers through the labyrinth of possibilities. k in the shadows, challenging the coder's resolve, yet debugging becomes a quest for perfection. 
Algorit(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)(null)

从上一课中停下来的地方继续阅读 导致文件的偏移量被更新

c offset file-descriptor lseek
2个回答
0
投票

文件只是存储数据。文件句柄存储读写偏移位置。

每个文件句柄都有自己的位置。


0
投票

lseek
索引位于文件描述符中。它不在文件中。 如果您想要一个索引,那么就拥有一个文件描述符。

© www.soinside.com 2019 - 2024. All rights reserved.