gpt4 book ai didi

C编程: write to file without buffer

转载 作者:太空狗 更新时间:2023-10-29 14:50:27 24 4
gpt4 key购买 nike

我正在使用fputs 将字符串写入文件,但是在 Debug模式下,语句fputs 后内容并没有写入磁盘。我认为有一些缓冲。但是我想通过直接查看内容来调试检查逻辑是否正确。无论如何禁用缓冲区?谢谢。

最佳答案

你有几个选择:

  • fflush(f); 在某个点刷新缓冲区。
  • setbuf(f, NULL); 禁用缓冲。

f 显然是你的 FILE*

即。

#include <stdio.h>

int main(void)
{
char s[100];

FILE *f = fopen("test.txt", "w");
setbuf(f, NULL);

while (fgets(s, 100, stdin))
fputs(s, f);

return 0;
}

#include <stdio.h>

int main(void)
{
char s[100];

FILE *f = fopen("test.txt", "w");

while (fgets(s, 100, stdin)) {
fputs(s, f);
fflush(f);
}

return 0;
}

关于C编程: write to file without buffer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8380696/

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