c read() 导致错误的文件描述符错误

标签 c file-descriptor

上下文是程序基本上一次读取 4K block 的文件流,寻找某种模式。它首先读取 4k block ,如果在那里找不到模式,它将启动一个循环,读取下一个 4k block (冲洗并重复,直到找到 EOF 或模式)。 在许多文件上,代码运行正常,但某些文件出现错误。

下面的代码显然经过高度编辑,我知道这可能很烦人,但它包含引用文件描述符或文件本身的所有行。我知道你不想相信我的话,因为我就是有问题的人......

在寻求帮助之前做了一点功课,我发现:

  1. 文件描述符恰好总是 = 6(对于正在工作的文件来说也是 6),并且该数字在执行周期内不会发生变化。不知道这是否有用。

  2. 通过在访问文件描述符的每个操作之后插入打印语句,我还发现成功的文件会经历以下循环“打开-读取-关闭-关闭”(即在第一个操作中找到该模式) 4K) 不成功的文件会“打开-读取-读取错误(错误文件描述符)-关闭”。因此,不会过早关闭,第一次读取会成功,但第二次读取会导致错误文件描述符错误。

.

int function(char *file)
{

int len, fd, go = 0;
char buf[4096];

if((fd = open(file, O_RDONLY)) <= 0)
{
    my_error("Error opening file %s: %s", file, strerror(errno));
    return NULL;
}

//first read
if((len = read(fd, buf, 4096)) <= 0)
{
    my_error("Error reading from file %s: %s", file, strerror(errno));
    close(fd); return NULL;
}

//pattern-searching

if(/*conditions*/)
{
    /* we found it, no need to keep looking*/
    close(fd);
}

else
{
    //reading loop
    while(!go)
    {
        if(/*conditions*/)
        {
            my_error("cannot locate pattern in file %s", file);
            close(fd); return NULL;
        }

        //next read
        if((len = read(fd, buf, 4096)) <= 0) /**** FAILS HERE *****/
        {
            my_error("Error reading from file, possible bad message %s: %s",
                file, strerror(errno));    
            close(fd); return NULL;
        }

        if(/*conditions*/)
        {
            close(fd);
            break;
        }

        //pattern searching

        if(/*conditions*/)
        {
             /* found the pattern */
            go++; //break us out of the while loop

            //stuff

            close(fd);
        }
        else
        {
            //stuff, and we will loop again for the next chunk
        }
    } /*end while loop*/
}/*end else statement*/

close(fd);
}

.

尽量不要担心模式读取逻辑 - 所有操作都是在字符缓冲区上完成的,而不是在文件上完成的,因此它应该对此问题没有影响。

最佳答案

EOF 返回 0(陷入 if ... <= 0),但不设置 errno,其中可能包含过时的代码。

尝试分别测试 0 和负(错误,-1)值。

<小时/>

关于“strace”:我在家里和以前的工作中使用过它一点。不幸的是,它没有安装在我当前的工作环境中。当它可用时,它是一个有用的工具。在这里,我对提问者采取了“让我们阅读精美手册”(man read)的方法:-)

关于c read() 导致错误的文件描述符错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6538363/

相关文章:

c - 读取一个文件并写入另一个 C

python - 用于 Windows 的 socket.fromfd

c++ - 为什么 UNIX 文件描述符不是由它们自己的类型实现的,尤其是在 C++ 中?

c - 非阻塞套接字和io

c - 解释为什么 C 中的条件变量不能正常工作

c - 为什么 pthread_join 在此示例中没有正确关闭线程数组?

c++ - Clang 文档页面中的代码片段

c - 收到错误 : Bad file descriptor: ***stack smashing detected*** at writing in client socket, 但套接字描述符正确

c - 在C中读取stdin文件描述符0中的值

linux - 关于 pthread_create 和文件描述符,我可以假设什么?