开发者

Efficient way to write multiple file content into a single file

开发者 https://www.devze.com 2023-03-23 05:41 出处:网络
There are n-number of files with vary in size. How we could 开发者_运维百科efficently append the content of all the files into a single file?

There are n-number of files with vary in size. How we could 开发者_运维百科efficently append the content of all the files into a single file?

Techniques or algorithm would help? Basically I am expecting efficent method to achieve this in c language.


Start simple. Multithreading will introduce significant complexity, and won't necessarily make things run any faster. Pseudocode time:

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"

BTW, this is dead simple (and near-optimal) to implement using simple command-line Linux tools (cat, etc.), though of couse that isn't exactly portable to Windows. One-liner example:

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

(Find every .txt file in the current directory and append it to result.out.)


Go through and find the total size of all of the files.

Then allocate an output file of that size, go through them again and write the data to your output.


Since I don't what the contents of the files are or the purpose of appending them, this solution might not be the best if its just text or something. However, I'd probably find a zip library to use (either licensed or open source), then just zip all the files into a single archive.

zlib looks interesting: http://www.zlib.net/


  1. get the size Sn of each file and calculate the total size T of all the files
  2. create the dest file
  3. use mmap to map the dest file with the size T, you will get a pointer P to the start address of the memmap region
  4. mmap each file to mem, and copy each data to the region above in order.
  5. after that, you would get the dest file with all the data from all the files
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号