learn-c-the-hard-way-lectures icon indicating copy to clipboard operation
learn-c-the-hard-way-lectures copied to clipboard

ex17.c

Open r3verser opened this issue 7 years ago • 4 comments

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.");
}

r3verser avatar Oct 24 '18 19:10 r3verser

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+");

r3verser avatar Oct 24 '18 19:10 r3verser

I'm having the same issue on fedora linux, but the rb+ didn't help

shaneu avatar Sep 04 '19 15:09 shaneu

There is no such mode as b+ afaik. Try to r+b instead: conn->file = fopen(filename, "r+b");

thegoodtheorist avatar Jul 17 '20 04:07 thegoodtheorist

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.

peter-chineham avatar Apr 17 '22 20:04 peter-chineham