HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcppMinor

C++ - create text file of given size

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
filecreatesizetextgiven

Problem

This code creates a text file and writes a given number of characters to it. Is this a good method, or is there a faster method?

#include 
#include 

std::ofstream file;

int main() {
    int size = 10000000; //~10MB
    file.open("text_file.txt");
    for (int i = 0; i < size; i++) {
        file << "a";
    }
    return 0;
}

Solution

A great deal here depends on what you want to accomplish.

If you want to ensure that the file occupies N bytes of storage space, then your current method may easily fail. Some file systems (e.g., NTFS) support file compression. Writing the same character many times gives highly compressible data, so as you've written it, the file is likely to use only a small amount of storage ("small" is relatively, but certainly a lot less than the ~10 megabytes you intended to write).

If you're all right with that sort of result, then you can probably do quite a bit better with something like this:

int main() {
    std::ofstream file("text_file.txt");
    file.seekp(10000000);
    file << 'a';
}


This will often be much faster than what you've written. It'll also typically occupy very little space on disk. A directory listing will still normally show it as being the same size though.

If you want to ensure that your file really uses ~10 megabytes of disk space, you'll probably need to fill it with semi-random data instead of just repeating a single value. One way to do this would be to use a random number generator:

std::mt19937 gen{ std::random_device()() };

std::uniform_int_distribution<> dis(0, 255);

std::ofstream file("text_file.txt");
std::generate_n(std::ostream_iterator(file, ""), size, [&]{ return dis(gen); });


Code Review

Globals vs. locals

There seems to be no real reason for making file a global variable as you did. Even if you're going to write to all the bytes of the file, it's almost certainly better to make it local to main (and initialize it upon creation):

int main() {
    int size = 10000000; //~10MB
    std::ofstream file("text_file.txt");
    [...]


Standard algorithms

Given that you're trying fill a container with a specified value, you might want to also consider using a standard algorithm designed for precisely that purpose:

std::ofstream file("text_file.txt");
    std::fill_n(std::ostream_iterator(file), size, 'n');


Use of headers

As it stands right now, you've included fstream.h, even though you don't seem to be using anything that's declared in that header. It's generally preferred to include only the headers you actually use.

Code Snippets

int main() {
    std::ofstream file("text_file.txt");
    file.seekp(10000000);
    file << 'a';
}
std::mt19937 gen{ std::random_device()() };

std::uniform_int_distribution<> dis(0, 255);

std::ofstream file("text_file.txt");
std::generate_n(std::ostream_iterator<char>(file, ""), size, [&]{ return dis(gen); });
int main() {
    int size = 10000000; //~10MB
    std::ofstream file("text_file.txt");
    [...]
std::ofstream file("text_file.txt");
    std::fill_n(std::ostream_iterator<char>(file), size, 'n');

Context

StackExchange Code Review Q#152554, answer score: 5

Revisions (0)

No revisions yet.