C fread()未成功返回

C fread()未成功返回,c,mingw,fread,C,Mingw,Fread,我正在学习教程“艰苦学习C”,似乎有一个名为Database_load的函数存在错误: void Database_load(struct Connection *conn) { int rc = fread(conn->db, sizeof(struct Database), 1, conn->file); if(rc != 1) die("Failed to load database."); } 函数返回“加载数据库失败” 我尝试使用调试器,并查看了fread

我正在学习教程“艰苦学习C”,似乎有一个名为Database_load的函数存在错误:

void Database_load(struct Connection *conn)
{
    int rc = fread(conn->db, sizeof(struct Database), 1, conn->file);
    if(rc != 1) die("Failed to load database.");
}
函数返回“加载数据库失败”

我尝试使用调试器,并查看了fread()文档,但我无法理解为什么此函数会成功返回

我重写了该函数以打印出一些测试以进行健全性检查:

void Database_load(struct Connection *conn)
{
    printf("Database_load(struct Connection *conn)\n");
    if (conn!=0)
    {
        printf("conn is not null\n");
        if (conn->file!=0)
        {
            printf("conn->file is not null\n");
        }//file is not null

    }//end conn is not null

    //actual read from filesystem 
    int rc = fread(conn->db, sizeof(struct Database), 1, conn->file);

        if (!conn->db!=0)
        {
            printf("conn->db is not null\n");
        }//db is not null

        if(ferror(conn->file))
        {
      printf("Error in reading from file\n");
        }

    if(rc != 1) die("Failed to load database.");
}
PS C:\Users\xyz\workspace_cpp\the_hard_way\ex17> .\ex_17.exe db.dat s 1 ary ary@yahoo.com
Database_load(struct Connection *conn)

conn is not null

conn->file is not null

ERROR: Failed to load database.
下面是命令输入:

void Database_load(struct Connection *conn)
{
    printf("Database_load(struct Connection *conn)\n");
    if (conn!=0)
    {
        printf("conn is not null\n");
        if (conn->file!=0)
        {
            printf("conn->file is not null\n");
        }//file is not null

    }//end conn is not null

    //actual read from filesystem 
    int rc = fread(conn->db, sizeof(struct Database), 1, conn->file);

        if (!conn->db!=0)
        {
            printf("conn->db is not null\n");
        }//db is not null

        if(ferror(conn->file))
        {
      printf("Error in reading from file\n");
        }

    if(rc != 1) die("Failed to load database.");
}
PS C:\Users\xyz\workspace_cpp\the_hard_way\ex17> .\ex_17.exe db.dat s 1 ary ary@yahoo.com
Database_load(struct Connection *conn)

conn is not null

conn->file is not null

ERROR: Failed to load database.
这是程序输出:

void Database_load(struct Connection *conn)
{
    printf("Database_load(struct Connection *conn)\n");
    if (conn!=0)
    {
        printf("conn is not null\n");
        if (conn->file!=0)
        {
            printf("conn->file is not null\n");
        }//file is not null

    }//end conn is not null

    //actual read from filesystem 
    int rc = fread(conn->db, sizeof(struct Database), 1, conn->file);

        if (!conn->db!=0)
        {
            printf("conn->db is not null\n");
        }//db is not null

        if(ferror(conn->file))
        {
      printf("Error in reading from file\n");
        }

    if(rc != 1) die("Failed to load database.");
}
PS C:\Users\xyz\workspace_cpp\the_hard_way\ex17> .\ex_17.exe db.dat s 1 ary ary@yahoo.com
Database_load(struct Connection *conn)

conn is not null

conn->file is not null

ERROR: Failed to load database.
我如何进一步探讨这个问题,是什么导致了这个问题?

想法:

  • 仔细检查打开
    conn->file
    的代码。(是否打开阅读,模式为“r”或“rb”?)
  • 初始设置数据库结构时,将conn->file设置为NULL;这将有助于抓住你忘记打开它的情况
  • 临时更改
    fread
    调用
    fread(conn->db,1,sizeof(struct Database),conn->file)
    并检查返回值;查看它是否大于0但小于
    sizeof(结构数据库)
  • 尝试调用
    getc(conn->file)
    查看是否获得EOF或字符
  • 一定要在错误发生后调用
    peror()
    或打印出
    strerror(errno)

  • 您可以使用
    perror
    了解出了什么问题。您可以检查
    conn!=0
    并且仍然取消对
    连接的引用
    ,如果它在书中出现,请停止阅读。