c - 为什么调用 lseek 后读取的文件总是返回 0?

标签 c linux ubuntu

我无法理解为什么在 lseek 后调用 read 返回读取的字节数为 0。

//A function to find the next note for a given userID;
//returns -1 if at the end of file is reached;
//otherwise, it returns the length of the found note.
int find_user_note(int fd, int user_uid) {
    int note_uid = -1;
    unsigned char byte;
    int length;

    while(note_uid != user_uid) { // Loop until a note for user_uid is found.
        if(read(fd, &note_uid, 4) != 4) // Read the uid data.
            return -1; // If 4 bytes aren't read, return end of file code.
        if(read(fd, &byte, 1) != 1) // Read the newline separator.
            return -1;

        byte = length = 0;
        while(byte != '\n') { // Figure out how many bytes to the end of line.
            if(read(fd, &byte, 1) != 1) // Read a single byte.
                return -1; // If byte isn't read, return end of file code.

            //printf("%x ", byte);
            length++;
        }
    }
    long cur_position = lseek(fd, length * -1, SEEK_CUR ); // Rewind file reading by length bytes.

    printf("cur_position: %i\n", cur_position);

    // this is debug
    byte = 0;
    int num_byte = read(fd, &byte, 1);

    printf("[DEBUG] found a %d byte note for user id %d\n", length, note_uid);
    return length;
}

存在外层while循环时变量长度值为34,上面代码产生cur_position 5(所以lseek函数返回后肯定至少有34个字节),但是函数read返回的变量num_byte总是返回0即使还有更多字节要读取。

有谁知道 num_byte 总是返回 0 的原因吗?如果这是我的代码中的错误,我看不出它是什么。

仅供引用,上面的代码是在下面的机器上运行的

$ uname -srvpio
Linux 3.2.0-24-generic #39-Ubuntu SMP Mon May 21 16:52:17 UTC 2012 x86_64 x86_64 GNU/Linux

更新:

  • 我上传完整代码here
  • 这是我尝试读取的文件内容
$ sudo hexdump -C /var/notes
00000000  e8 03 00 00 0a 74 68 69  73 20 69 73 20 61 20 74  |.....this is a t|
00000010  65 73 74 20 6f 66 20 6d  75 6c 74 69 75 73 65 72  |est of multiuser|
00000020  20 6e 6f 74 65 73 0a                              | notes.|
00000027

$

最佳答案

如果 length 是一个小于 off_t 的无符号类型(例如,32 位机器上的 size_t),那么 length*-1 将是一个巨大的值(大约 4GB 左右)。这可能是问题所在。将 lseek 的结果存储到 long(同样,如果它是 32 位)将应用实现定义的转换,可能是截断,这再次为您留下一个小值.

我看到你的机器是 64 位的,但也许你运行的是 32 位用户空间?

无论如何,为什么不在 strace 下运行您的程序,看看它进行了哪些系统调用?这几乎肯定会很快解决问题。

关于c - 为什么调用 lseek 后读取的文件总是返回 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10966218/

相关文章:

i2cdump/i2cget 可以在 C 可执行文件中使用吗?

linux - Ubuntu - 无法纠正问题,你持有损坏的软件包

c - 当我们在 C 中取消引用 NULL 指针时,OS 中会发生什么?

linux - 无法连接到 22 (ssh) AWS 以外的任何端口

sql - bash 脚本 curl 命令中的单引号不断转换为双引号

python - 使用 PuTTY 或 plink 通过 python 子进程连接到远程 linux 机器会抛出错误

c++ - 如果整数溢出, (unsigned int) * (int) 的结果是什么?无符号或整数?

c - 为什么在 32 位系统中包含一个 int64 变量时结构大小是 8 的倍数

C:为什么额外的分号可以?

linux - 在/etc/apt/sources.list.d/中创建一个文件