C 中的字符串流

发布于 2024-09-14 10:53:07 字数 182 浏览 7 评论 0原文

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

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

 return 0;
}

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

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

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

 return 0;
}

How to write the data into string stream instead of File stream?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情绪 2024-09-21 10:53:07

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

There is a very neat function in the posix 2008 standard: open_memstream(). You use it like this:

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);
初与友歌 2024-09-21 10:53:07

使用sprintfhttp://www.cplusplus.com/reference /cstdio/sprintf/

这是参考中的示例:

#include <stdio.h>

int main ()
{
  char buffer [50];
  int n, a=5, b=3;
  n = sprintf(buffer, "%d plus %d is %d", a, b, a+b);
  printf("[%s] is a string %d chars long\n", buffer, n);
  return 0;
}

输出:

[5 plus 3 is 8] is a string 13 chars long

根据注释中的建议进行更新:

使用 snprintf 因为它更安全(它可以防止缓冲区溢出攻击)并且更便携。

#include <stdio.h>

int main ()
{
  int sizeOfBuffer = 50;
  char buffer[sizeOfBuffer];
  int n, a = 5, b = 3;
  n = snprintf(buffer, sizeOfBuffer, "%d plus %d is %d", a, b, a+b);
  printf ("[%s] is a string %d chars long\n", buffer, n);
  return 0;
}

请注意,snprintf 的第二个参数实际上是允许使用的最大大小,因此您可以将其设置为低于 sizeOfBuffer 的值,但是对于您的情况,这是不必要的。 snprintf 仅写入 sizeOfBuffer-1 个字符,并使用最后一个字节作为终止字符。

以下是 snprintf 文档的链接:http://www .cplusplus.com/reference/cstdio/snprintf/

Use sprintf: http://www.cplusplus.com/reference/cstdio/sprintf/

Here's an example from the reference:

#include <stdio.h>

int main ()
{
  char buffer [50];
  int n, a=5, b=3;
  n = sprintf(buffer, "%d plus %d is %d", a, b, a+b);
  printf("[%s] is a string %d chars long\n", buffer, n);
  return 0;
}

Output:

[5 plus 3 is 8] is a string 13 chars long

Update based on recommendations in comments:

Use snprintf as it is more secure (it prevents buffer overflow attacks) and is more portable.

#include <stdio.h>

int main ()
{
  int sizeOfBuffer = 50;
  char buffer[sizeOfBuffer];
  int n, a = 5, b = 3;
  n = snprintf(buffer, sizeOfBuffer, "%d plus %d is %d", a, b, a+b);
  printf ("[%s] is a string %d chars long\n", buffer, n);
  return 0;
}

Notice that snprintf's second argument is actually the max allowed size to use, so you can put it to a lower value than sizeOfBuffer, however for your case it would be unnecessary. snprintf only writes sizeOfBuffer-1 chars and uses the last byte for the termination character.

Here is a link to the snprintf documentation: http://www.cplusplus.com/reference/cstdio/snprintf/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文