C 在遇到一定数量的换行符后,我的程序会返回我想要的正确位置吗?

C 在遇到一定数量的换行符后,我的程序会返回我想要的正确位置吗?,c,linux,ubuntu-18.04,low-level-io,C,Linux,Ubuntu 18.04,Low Level Io,我试图向后读取一个文件(比如文件末尾的10行)。每次读到“\n”时,我都会增加我的新行计数器(newline\u counter)。一旦newline\u计数器到达user\u num(参数),比如说10行,lseek()将在当前位置停止(current\u pos)。我返回此位置,以便在另一个函数中使用此位置,该函数使用lseek()到此位置,并从该位置开始读取和写入,直到文件结束。我已经成功地编译了这个程序,但是一旦我开始运行它,程序就一直在运行,没有输出 int func_line_mod

我试图向后读取一个文件(比如文件末尾的10行)。每次读到“\n”时,我都会增加我的新行计数器(
newline\u counter
)。一旦
newline\u计数器
到达
user\u num
(参数),比如说10行,
lseek()
将在当前位置停止(
current\u pos
)。我返回此位置,以便在另一个函数中使用此位置,该函数使用
lseek()
到此位置,并从该位置开始读取和写入,直到文件结束。我已经成功地编译了这个程序,但是一旦我开始运行它,程序就一直在运行,没有输出

int func_line_mode(int infile, int user_num) {
    char c;
    int newline_counter = 0;
    int current_pos = 0;
    int end = lseek(infile, 0, SEEK_END);

    int counter = 0;

    while (counter < end || newline_counter <= user_num) {
        lseek(infile, current_pos, SEEK_END);
        read(infile, &c, sizeof(char));
        if (strcmp(&c,"\n") == 0) {
            newline_counter++;
        }
        current_pos--;
        counter++;
    }

    return current_pos;
}
int func\u line\u模式(int infle,int user\u num){
字符c;
int换行符_计数器=0;
int current_pos=0;
int end=lseek(填充,0,搜索结束);
int计数器=0;

而(counter
  • 条件错误时,应为:

    while (counter < end && newline_counter <= user_num)
    
  • lseek()

    off_t end = lseek(infile, 0, SEEK_END);
    
  • 因此,用于进行比较的其他变量也应该是
    off\t
    ,最重要的是函数的返回类型

  • strcmp(&c,“\n”)
    是错误的,要比较单个字符,只需执行
    c='\n'

  • 1号可能是你问题的原因。其他问题也应该解决,特别是4号


    一旦上述所有问题得到解决,该函数就可以正常工作。下面是一个工作示例:

    #include <sys/types.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <stdio.h>
    
    off_t func_line_mode(int infile, int user_num) {
        char c;
        int newline_counter = 0;
        off_t current_pos = 0;
        off_t end = lseek(infile, 0, SEEK_END);
        off_t counter = 0;
    
        while (counter < end && newline_counter < user_num) {
            lseek(infile, current_pos, SEEK_END);
            read(infile, &c, 1);
    
            if (c == '\n')
                newline_counter++;
    
            current_pos--;
            counter++;
        }
    
        if (current_pos < 0)
            current_pos += 2;
    
        return current_pos;
    }
    
    int main() {
        char buf[100];
        int nread, nwrite;
    
        int fd = open("test.txt", O_RDONLY);
    
        // Last 3 lines.
        off_t off = func_line_mode(fd, 3);
    
        printf("off = %d\n", off);
    
        // Go back.
        lseek(fd, off, SEEK_END);
    
        while (nread = read(fd, buf, 100)) {
            nwrite = 0;
    
            while (nwrite < nread)
                nwrite += write(1, buf + nwrite, nread - nwrite);
        }
    
        return 0;
    }
    
    #包括
    #包括
    #包括
    #包括
    关闭函数行模式(整数填充,整数用户数){
    字符c;
    int换行符_计数器=0;
    关断电流位置=0;
    off_t end=lseek(填充,0,搜索_end);
    关闭计数器=0;
    while(计数器
    @user3121023好的,我更改了它。但是程序仍然没有输出任何东西。
    &c
    不是指向有效字符串的指针,所以使用strcmp(&c,
    是非法的。什么程序?上面的函数没有输出到控制台。你不应该在(countermain()?啊,是main()中的那个),很抱歉没有指定。这只是一种标准方法,可以一直读取到文件结尾,然后将所有内容写入标准输出。
    read()
    不能保证读取所有100字节,因此我确实要
    nread=read(…)
    来确切知道读取了多少字节。同样,
    write()
    不保证写入所有
    nread
    字节,因此我一直使用循环写入,直到写入所有
    nread
    字节。当
    read()
    返回
    0
    (表示到达文件结尾),外部循环退出。我刚刚快速编写了一个工作示例,当然您还应该检查使用
    read()
    write()
    时的错误,我的代码没有这样做。
    #include <sys/types.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <stdio.h>
    
    off_t func_line_mode(int infile, int user_num) {
        char c;
        int newline_counter = 0;
        off_t current_pos = 0;
        off_t end = lseek(infile, 0, SEEK_END);
        off_t counter = 0;
    
        while (counter < end && newline_counter < user_num) {
            lseek(infile, current_pos, SEEK_END);
            read(infile, &c, 1);
    
            if (c == '\n')
                newline_counter++;
    
            current_pos--;
            counter++;
        }
    
        if (current_pos < 0)
            current_pos += 2;
    
        return current_pos;
    }
    
    int main() {
        char buf[100];
        int nread, nwrite;
    
        int fd = open("test.txt", O_RDONLY);
    
        // Last 3 lines.
        off_t off = func_line_mode(fd, 3);
    
        printf("off = %d\n", off);
    
        // Go back.
        lseek(fd, off, SEEK_END);
    
        while (nread = read(fd, buf, 100)) {
            nwrite = 0;
    
            while (nwrite < nread)
                nwrite += write(1, buf + nwrite, nread - nwrite);
        }
    
        return 0;
    }