learn-c-the-hard-way-lectures
learn-c-the-hard-way-lectures copied to clipboard
ex17.c
Tried this lesson, but got error. Have retyped it from book, but same thing happens! Could somebody help me with it?:zipper_mouth_face: Im on Windows.
app.exe db.dat c
Creates file db.dat 101kb size
app.exe db.dat s 1 zed [email protected]
Fires
ERROR: Failed to load database.
Debugger said it happends at fread(), it returns 0 somehow
int rc = fread(conn->db, sizeof(struct Database), 1, conn->file);
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.");
}
For posterity
The problem was in Database_open()
On Windows when opening file in binary mode we should provide not 'r+' but 'rb+', like so:
conn->file = fopen(filename, "rb+");
I'm having the same issue on fedora linux, but the rb+ didn't help
There is no such mode as b+ afaik.
Try to r+b instead:
conn->file = fopen(filename, "r+b");
For posterity
The problem was in Database_open() On Windows when opening file in binary mode we should provide not
'r+' but'rb+', like so:conn->file = fopen(filename, "rb+");
In Debian, fopen works as in the original source. On Windows VS and MSDOS (GNU) I need rb+ as stated for it to work.