gpt4 book ai didi

c - 在 C 上保存文件

转载 作者:太空宇宙 更新时间:2023-11-04 06:07:57 25 4
gpt4 key购买 nike

我想在电脑上保存文件。我可以使用 fwrite 命令。

但是我必须在使用 fwrite 命令保存时在 for 循环中枚举文件,如 file01、file02、..。

所以我必须保存;例如十个文件(file01、fle02、file03....、file10...)

你能给我一个简单的示例代码吗?

最佳答案

在循环中你需要

  1. 构建文件名
  2. 打开文件
  3. 写入数据
  4. 关闭文件

C99 示例(snprintf() 是“新的”),省略了很多细节

for (j = 0; j < 10; j++) {
snprintf(buf, sizeof buf, "file%02d.txt", j + 1); /* 1. */
handle = fopen(buf, "w"); /* 2. */
if (!handle) /* error */ exit(EXIT_FAILURE); /* 2. */
w = fwrite(data, 1, bytes, handle); /* 3. */
if (w != bytes) /* check reason */; /* 3. */
fclose(handle); /* 4. */
}

关于c - 在 C 上保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5902923/

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