Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to create large file in c++?

Create a flat text file in c++ around 50 - 100 MB with the content 'Added first line' should be inserted in to the file for 4 million times

like image 965
yesraaj Avatar asked Nov 05 '08 15:11

yesraaj


2 Answers

using old style file io

fopen the file for write.

fseek to the desired file size - 1.

fwrite a single byte

fclose the file

like image 162
EvilTeach Avatar answered Oct 04 '22 06:10

EvilTeach


The fastest way to create a file of a certain size is to simply create a zero-length file using creat() or open() and then change the size using chsize(). This will simply allocate blocks on the disk for the file, the contents will be whatever happened to be in those blocks. It's very fast since no buffer writing needs to take place.

like image 36
Ferruccio Avatar answered Oct 04 '22 06:10

Ferruccio