gpt4 book ai didi

c - 如何将字符串插入缓冲区?

转载 作者:行者123 更新时间:2023-11-30 14:24:43 25 4
gpt4 key购买 nike

我是 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);
}

问题是如何将字符串插入循环缓冲区,然后将它们写入文本文件?此实现仅适用于数字。

最佳答案

有两种方法:

  1. 在循环缓冲区中插入指针。您会将字符串存储在内存中的某个位置。然后你就有了一个指向内存中该位置的指针,并将其存储在循环缓冲区中。

  2. 将字符存储在循环缓冲区中,以便将它们连接起来形成字符串。这意味着字符串被复制到缓冲区中,并且您需要某种方法从缓冲区中读取足够的字符来组成字符串。

关于c - 如何将字符串插入缓冲区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11247381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com