Files & storageSDL 3.2

SDL_LoadFile

Reads a whole file into memory in one call.

Signature

void * SDL_LoadFile(const char *file, size_t *datasize);

How it works

Good for level files, JSON, shaders. SDL adds a zero byte after the data, so a text file can be used directly as a C string. The size you get back does not count that extra byte.

On Android it can also read files packed inside the APK, which plain fopen cannot.

Parameters

fileconst char *

The path to read.

datasizesize_t *

Receives the number of bytes read, or NULL.

Return value

The data (free with SDL_free), or NULL on failure.

Example

example.cpp
1size_t size = 0;
2char* json = static_cast<char*>(SDL_LoadFile("assets/level1.json", &size));
3if (!json) { SDL_Log("LoadFile: %s", SDL_GetError()); return; }
4parseLevel(std::string_view(json, size));
5SDL_free(json);
Official SDL wiki