This is new wiki software and old wiki content. It's a work in progress!
Here's the explanation.
Be gentle, report bugs, leave feedback on pages, or just edit them yourself! Thanks!

SDL Wiki

SDL_RWread

Read from a data source.

Syntax

size_t SDL_RWread(SDL_RWops *context,
                  void *ptr, size_t size,
                  size_t maxnum);

Function Parameters

context

a pointer to an SDL_RWops structure

ptr

a pointer to a buffer to read data into

size

the size of each object to read, in bytes

maxnum

the maximum number of objects to be read

Return Value

Returns the number of objects read, or 0 at error or end of file; call SDL_GetError() for more information.

Remarks

This function reads up to maxnum objects each of size size from the data source to the area pointed at by ptr. This function may read less objects than requested. It will return zero when there has been an error or the data stream is completely read.

SDL_RWread() is actually a function wrapper that calls the SDL_RWops's read method appropriately, to simplify application development.

Code Examples

SDL_RWops *rw = SDL_RWFromFile("test.bin","r");
if (rw != NULL) {
    extern Uint8 buf[256];
    SDL_RWread(rw, buf, sizeof (buf), 1);
    SDL_RWclose(rw);
}

Read a complete file in memory (assuming size can be detected) (from OpenGL WikiBook):

char* file_read(const char* filename) {
    SDL_RWops *rw = SDL_RWFromFile(filename, "rb");
    if (rw == NULL) return NULL;

    Sint64 res_size = SDL_RWsize(rw);
    char* res = (char*)malloc(res_size + 1);

    Sint64 nb_read_total = 0, nb_read = 1;
    char* buf = res;
    while (nb_read_total < res_size && nb_read != 0) {
        nb_read = SDL_RWread(rw, buf, 1, (res_size - nb_read_total));
        nb_read_total += nb_read;
        buf += nb_read;
    }
    SDL_RWclose(rw);
    if (nb_read_total != res_size) {
        free(res);
        return NULL;
    }

    res[nb_read_total] = '\0';
    return res;
}
SDL_RWclose
SDL_RWFromConstMem
SDL_RWFromFile
SDL_RWFromFP
SDL_RWFromMem
SDL_RWseek
SDL_RWwrite

CategoryAPI, CategoryIO


[ edit | delete | history | feedback | raw ]

[ front page | index | search | recent changes | git repo | offline html ]

All wiki content is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0).
Wiki powered by ghwikipp.