Question

I'm new to C programming. I want to create circular buffer which writes strings into text file.

#include <stdio.h>
#include <malloc.h>

// Buffer writer header files
#include <stdlib.h>

typedef struct { char value; } ElemType;

/* Circular buffer object */
typedef struct {
    int         size;   /* maximum number of elements           */
    int         start;  /* index of oldest element              */
    int         end;    /* index at which to write new element  */
    ElemType   *elems;  /* vector of elements                   */
} CircularBuffer;

void cbInit(CircularBuffer *cb, int size);
void cbFree(CircularBuffer *cb);
int cbIsFull(CircularBuffer *cb);
int cbIsEmpty(CircularBuffer *cb);
void cbWrite(CircularBuffer *cb, ElemType *elem);
void cbRead(CircularBuffer *cb, ElemType *elem);
void writeFile();


void cbInit(CircularBuffer *cb, int size) {
    cb->size  = size + 1; /* include empty elem */
    cb->start = 0;
    cb->end   = 0;
    cb->elems = (ElemType *)calloc(cb->size, sizeof(ElemType));
}

void cbFree(CircularBuffer *cb) {
    free(cb->elems); /* OK if null */ }

int cbIsFull(CircularBuffer *cb) {
    return (cb->end + 1) % cb->size == cb->start; }

int cbIsEmpty(CircularBuffer *cb) {
    return cb->end == cb->start; }

/* Write an element, overwriting oldest element if buffer is full. App can
   choose to avoid the overwrite by checking cbIsFull(). */
void cbWrite(CircularBuffer *cb, ElemType *elem) {
    cb->elems[cb->end] = *elem;
    cb->end = (cb->end + 1) % cb->size;
    if (cb->end == cb->start)
        cb->start = (cb->start + 1) % cb->size; /* full, overwrite */
}

/* Read oldest element. App must ensure !cbIsEmpty() first. */
void cbRead(CircularBuffer *cb, ElemType *elem) {
    *elem = cb->elems[cb->start];
    cb->start = (cb->start + 1) % cb->size;
}

int mainSecond(int argc, char **argv) {
    CircularBuffer cb;
    ElemType elem = {0};

    int testBufferSize = 10; /* arbitrary size */
    cbInit(&cb, testBufferSize);

    /* Fill buffer with test elements 3 times */
    for (elem.value = 0; elem.value < 3 * testBufferSize; ++ elem.value)
        cbWrite(&cb, "AC");

    /* Remove and print all elements */
    while (!cbIsEmpty(&cb)) {
        cbRead(&cb, &elem);
        printf("%d\n", elem.value);
    }

    cbFree(&cb);
    return 0;
}



int main(int argc, char** argv) {

    writeFile();

    return 0;
}

// write to file function

void writeFile() {

    FILE *file;
    file = fopen("file.txt", "a+"); /* apend file (add text to a file or create a file if it does not exist.*/

    CircularBuffer cb;
    ElemType elem = {0};

    int testBufferSize = 10; /* arbitrary size */
    cbInit(&cb, testBufferSize);

    /* Fill buffer with test elements 3 times */
    for (elem.value = 0; elem.value < 3 * testBufferSize; ++ elem.value)
        cbWrite(&cb, "test");

    /* Remove and print all elements */
    while (!cbIsEmpty(&cb)) {
        cbRead(&cb, &elem);
        fprintf(file, "%d\n", elem.value);
       // printf("%d\n", elem.value);      
    }   

    // write something into the file
    fprintf(file, "%s", "Test!\n");
    // close the file
    fclose(file); 
    //getchar(); /* pause and wait for key */
    cbFree(&cb);
}

The question is how I can insert Strings into the circular buffer and after that write them into text file? This implementation only works with numbers.

Was it helpful?

Solution

There are two ways:

  1. Insert pointers in the circular buffer. You would store your string somewhere in memory. Then you have a pointer to that place in memory, that you store in your circular buffer.

  2. Store characters in your circular buffer, in such a way that concatenating them results in strings. This means that the strings are copied into your buffer, and you need some way to read enough characters out of the buffer to make up a string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top