2012-06-28 32 views
0

我是C編程新手。我想創建循環緩衝區,將字符串寫入文本文件。如何將字符串插入緩衝區?

#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); 
} 

問題是我如何將字符串插入到循環緩衝區,然後將它們寫入文本文件?此實現僅適用於數字。

回答

2

有兩種方式:

  1. 在循環緩衝區插入指針。你會將你的字符串存儲在內存中。然後你有一個指向內存中的那個地方,你存儲在你的循環緩衝區。

  2. 將字符存儲在循環緩衝區中,以連接它們的方式生成字符串。這意味着字符串被複制到緩衝區中,並且需要某種方式從緩衝區中讀取足夠多的字符以組成字符串。

+0

例如,我可以擴展循環緩衝區來存儲字符串,這是不可能的嗎? – user1285928

+0

將ElemType更改爲包含'char *'(= string)而不是'int',然後可以將字符串添加到緩衝區。 – Sjoerd

+0

我更新了代碼,但在嘗試編譯代碼時出現錯誤。看來我不能直接插入字符串。你能告訴我什麼是將字符串插入緩衝區的正確方法嗎? – user1285928