Handmade Hero»Forums»Code
Jesse Coyle
37 posts
Using Casey's File I/O
Does anyone know how to use Casey's File I/O, or tell me if it is even capable of what I'm trying to do here?

I'm basically trying to store an array of ints into a file. Though the void *'s are sort of foreign to me. I thought you could cast things down easily between void * and other data types like ints or array of ints, or floats etc...

tl;dr: How do I use Casey's File I/O functions to put an array of ints into a file and read from that file.

I've read msdn a bit, yet like always there's no straight answer from what I can tell. And I would use fstream, but I'm getting warnings from an xlocale file (which is run as an error). I guess either help would do, and I could just turn off -WX compiler switch.
Mārtiņš Možeiko
2562 posts / 2 projects
Using Casey's File I/O
You can not use C++ I/O classes (fstream) in Handmade project, because it disabled C++ exceptions ("-EHa-" compiler argument). Either remove this argument so C++ exception are allowed, or use C file I/O (fopen/fread/fwrite).

To use debug I/O functions Casey implemented, do this:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
int something[] = { 1, 2, 3 };

bool32 ok = DEBUGPlatformWriteEntireFile("debug.txt", something, sizeof(something));
Assert(ok);

// ...  later read it back:
int array[3];

debug_read_file_result fresult = DEBUGPlatformReadEntireFile("debug.txt");
if (fresult.Contents != 0)
{
    Assert(fresult.ContentsSize == sizeof(array));
    memcpy(array, fresult.Contents);
    DEBUGPlatformFreeFileMemory(fresult.Contents);
}

// use array here
// ...


Instead of memcpy you could cast it to array type and use it directly:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// ...  later read it back:
int* array = 0;

debug_read_file_result fresult = DEBUGPlatformReadEntireFile("debug.txt");
Assert(fresult.Contents != 0); // check that file is loaded

// use array here
// ...

// later close it
DEBUGPlatformFreeFileMemory(fresult.Contents);
Jesse Coyle
37 posts
Using Casey's File I/O
hmm. well getting rid of the -EHa- switch didn't help as far as fstream, got the same warning, must have been a different problem.

But I'll just use the C File I/O, maybe someday when Casey goes over File I/O more (I haven't gotten to his BMP loading episode yet, he might add more then) then I'll use something with more close to the core.

Thanks man.
Mārtiņš Možeiko
2562 posts / 2 projects
Using Casey's File I/O
Oh, my bad - instead of removing "-EHa-" you need to change it to "-EHsc".
Jesse Coyle
37 posts
Using Casey's File I/O
still no dice, even if I remove that switch completely it doesn't get rid of the warning. Doesn't matter to much, you've still been very helpful.
Casey Muratori
801 posts / 1 project
Casey Muratori is a programmer at Molly Rocket on the game 1935 and is the host of the educational programming series Handmade Hero.
Using Casey's File I/O
The file I/O that's in there now is definitely not what we will use in the end because we will be doing a bunch of async stuff, but it is the same basic principles: file I/O is about writing blocks of memory to disk. You should get comfortable with being able to think of your array of integers as a block of memory, and you should understand how to read and write that chunk of memory. It is very easy to do with the existing file routines as-is: the memory for the integers is a region of memory. It has a size. To write it, you need a pointer to the beginning (which you have - it's just the array pointer) and the size (which you have - it's the count times sizeof(int)). That's all you need!

- Casey
Jesse Coyle
37 posts
Using Casey's File I/O
I see from mmozeiko's first post there on how to use it. Using Lua & Löve2d for 2 years, then drastically going lower level is a bit unsettling, but I bring my very high level programming skills with me (as in scripting level, I'm no master yet).

I had a feeling that it could be as simple as casting the array into a void * and then pass it in, but it looks like it's even easier as just putting in the array and it's size for the write.

The reason I never used it is when I tried mmozeiko's code there, it couldn't find what memcpy was, so to get my assignment in on time (at least not later than it already was), I had to go with something I was more experienced with, and being that had to be higher level with the C I/O rather than finding out why it didn't know what memcpy was.

what are you doing up at 2:30 my friend?