cfwrite

How to create a similar function of fwrite


As you read in the title, I want to create a function like fwrite. But I can't seem to get a function similar to fwrite. I tried this:

int mini_fwrite(void *buffer, int size_element, int number_element, MYFILE *file) {
    if (file->ind_write == -1) {
        file->buffer_write = mini_calloc(IOBUFFER_SIZE, 1);
        file->ind_write = 0;
    }

    int i = 0;
    while (i < size_element * number_element) {
        ((char *)file->buffer_write)[file->ind_write] = ((char *)buffer)[i];
        file->ind_write++;
        i++;
        
        if (file->ind_write == IOBUFFER_SIZE) {
            if (write(file->fd, file->buffer_write, file->ind_write) == -1) {
                mini_perror("Error");
            }
            file->ind_write = 0;
        }
    }
    return i;
}

That's not what I want. I want writing, via a write, only be triggered when the buffer_write is full. Anyone have an idea ?

I started with a structure like this

typedef struct MYFILE {
    int fd;
    void *buffer_read;
    void *buffer_write;
    int ind_read;
    int ind_write;
} MYFILE;

and a function like fopen:

int IOBUFFER_SIZE = 2048;

MYFILE *mini_fopen(char *file, char mode)
{
    if (file == NULL)
        return NULL;

    MYFILE *fo;

    fo = (MYFILE *)mini_calloc(sizeof(MYFILE), 1);

    if (mode == 'r') fo->fd = open(file, O_RDONLY);
    else if (mode == 'w') fo->fd = open(file, O_WRONLY);
    else if (mode == 'b') fo->fd = open(file, O_RDWR);
    else if (mode == 'a') fo->fd = open(file, O_APPEND);
    else mini_perror("Error\n");

    if (fo->fd == -1) return NULL;
    fo->ind_read = -1;
    fo->ind_write = -1;
    fo->buffer_read = NULL;
    fo->buffer_write = NULL;

    return fo;
}

Solution

  • I want writing, via a write, only be triggered when the buffer_write is full.

    Your function does exactly that. It allocates the output buffer if not allocated already and copies bytes one at a time to it, writing the contents when file->ind_write has reached IOBUFFER_SIZE.

    Yet there are some problems: