How to quickly create large files in C?

孤者浪人 提交于 2020-06-17 13:12:46

问题


I am doing research on file system performance, and I am stumped on how to create a very large file very quickly in C. Basically, I am trying to re-create a file system's folders and files by taking this metadata and storing it into a file. This is the extraction process. Later, I want to restore those folders and files into an existing freshly-made file system (in this case, ext3) using the metadata I previously extracted.

In the restore process, I have already succeeded in creating all the folders. However, I am a little confused on how to create the files instantly. For every file that I want to create, I have a file size and a file path. I am just confused on how to set the size of the file very quickly.

I used truncate, but this does not seem to affect the disk space usage from the point of view of the file system.

Thanks!


回答1:


There is no way to do it instantly.

You need to have each block of the file written on disk and this is going to take a significant period of time, especially for a large file.




回答2:


#include < stdio.h >
#include < stdlib.h >

int main() {
    int i;
    FILE *fp;

    fp=fopen("bigfakefile.txt","w");

    for(i=0;i<(1024*1024);i++) {
        fseek(fp,(1024*1024), SEEK_CUR);
        fprintf(fp,"C");
    }

    fclose(fp);
    return 0;
}


来源:https://stackoverflow.com/questions/16776565/how-to-quickly-create-large-files-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!