gpt4 book ai didi

c - 将多个文件内容写入单个文件的有效方法

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:32:59 25 4
gpt4 key购买 nike

有 n 个大小不同的文件。我们如何才能有效地将所有文件的内容附加到一个文件中?

技术或算法会有帮助吗?基本上我期待用 C 语言实现这一点的有效方法。

最佳答案

从简单开始。多线程会引入显着的复杂性,并且不一定会使事情运行得更快。伪代码时间:

Create a new file "dest" in write-only mode.
For each file "source" you want to append:
Open "source" in read-only mode
For each line "L" in "source":
Write "L" to "dest"
Close "source"
Close "dest"

顺便说一句,使用简单的命令行 Linux 工具(cat 等)来实现它非常简单(并且接近最佳),尽管它不能完全移植到 Windows。单行示例:

for i in `find . -type f -name "*.txt"`; do cat $i >> result.out; done

(查找当前目录中的每个 .txt 文件并将其附加到 result.out。)

关于c - 将多个文件内容写入单个文件的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6853899/

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