Register
Handmade Hero»Forums»Code»Questions about file handling
69 posts
Questions about file handling
4 months, 2 weeks ago Edited by C_Worm on Feb. 23, 2020, 9:11 p.m. Reason: Initial post
Hi, im wondering what is commonly used as file handlig.

Is the C stdio.h the way to go with file handling or are there any other commonly used File handling libraries?

Should you write your own file handling system and how would you go about doing so?

What are bigger studios using when it comes to this subject?

Are there any alternatives to stdio.h if you want to program in C.
Matt
2 posts
Questions about file handling
4 months, 2 weeks ago Edited by Matt on Feb. 23, 2020, 10:27 p.m.
For game engines, people tend to avoid the CRT / STL.
The reason is that most of the time they're too general, thus sub-optimal for the given situation.

If you're using windows, you want to use CreateFileA, ReadFile, etc.

Every situation has its own specifics though, so depending on that you may want to:
- Use overlapped IO.
- Memory Map the file.

Here's the relevant episode where Casey explains things in more depth.
https://hero.handmade.network/episode/code/day152

As for libraries, it depends on what you want to do exactly.
If you want to load images / textures / resources, there's probably an stb / stb-style
library. Those are always really good.

If it's for general purpose file-loading / file-writing, it's probably worth just rolling your own code.

Hope it helps!
Mārtiņš Možeiko
2090 posts / 1 project
Questions about file handling
4 months, 2 weeks ago Edited by Mārtiņš Možeiko on Feb. 23, 2020, 10:25 p.m.
For simple stuff stdio file handling works fine. Just avoid STL, iostream performance is terrible.
But once you want to do something more, it won't be enough. You'll need to use platform specific native file API. On Windows it is CreateFile & friends, on Linux/macOS it is open syscall. That will give you more control, for example, memory mapped files, reading/writing at offset without seeking, even async I/O. Consoles usually have their own API.
Big game engines typically have layers over layers over file API that wraps things in a way that it is easier to use in engine framework.
59 posts

None

Questions about file handling
4 months, 2 weeks ago Edited by albatros on Feb. 25, 2020, 3:27 p.m.
mmozeiko
on Linux/macOS it is open syscall. That will give you more control, for example, memory mapped files, reading/writing at offset without seeking, even async I/O. Consoles usually have their own API.


Just to be sure, how do you read/write without seeking on linux ? you use mmap ?

None
482 posts
Questions about file handling
4 months, 2 weeks ago
albatros
mmozeiko
on Linux/macOS it is open syscall. That will give you more control, for example, memory mapped files, reading/writing at offset without seeking, even async I/O. Consoles usually have their own API.


Just to be sure, how do you read/write without seeking on linux ? you use mmap ?


you can use pread which adds a file offset to the parameter list