gpt4 book ai didi

c - C 中的字符串流

转载 作者:太空狗 更新时间:2023-10-29 16:41:13 25 4
gpt4 key购买 nike

print2fp(const void *buffer, size_t size, FILE *stream) {

if(fwrite(buffer, 1, size, stream) != size)
return -1;

return 0;
}

如何将数据写入字符串流而不是文件流?

最佳答案

posix 2008 标准中有一个非常简洁的函数:open_memstream ().你可以这样使用它:

char* buffer = NULL;
size_t bufferSize = 0;
FILE* myStream = open_memstream(&buffer, &bufferSize);

fprintf(myStream, "You can output anything to myStream, just as you can with stdout.\n");
myComplexPrintFunction(myStream); //Append something of completely unknown size.

fclose(myStream); //This will set buffer and bufferSize.
printf("I can do anything with the resulting string now. It is: \"%s\"\n", buffer);
free(buffer);

关于c - C 中的字符串流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3481157/

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