Celeritas
Celeritas

Reputation: 15053

Why do two files get written to before the third one ever does even though the instructions are to do each one per iteration?

This program writes one line of data to three files once per iteration of the loop. Watching the file size increase, I notice they don't all grow at the same time. Usually two files grow and one remains close to zero bytes until the end. Why is this? I assume some sort of optimization is taking place; is it easier for a hard drive to finish writing two files than more?

#include <fstream>
#include <iostream>
#include <stdlib.h>//for rand and srand
#include <time.h>//for time()

using namespace std;

int main()
{
    srand(time(NULL));
    ofstream a1("f:\\test\\first.txt");
    ofstream b1("f:\\test\\second.txt");
    ofstream c1("f:\\test\\third.txt");

    if(!a1 || !b1 || !c1)
    {
        cerr << "Cannot open the output files." << endl;
        return 1;
    }
   cout << "Program has started:" << endl;
    for(int i = 0; i < 50000000; i++)
    {
        a1<<rand() % 1000000 + 1;
        a1<<"adsfgijasdflhkzxoijasdd";
        a1<<"\n";
        b1<<rand() % 1000000 + 1;
        b1<<"kjhkjhkjhkhftfzxdtdtdliu";
        b1<<"\n";
        c1<<rand() % 1000000 + 1;
        c1<<"kjlkjoijsegoihsdfoinsvfglkjhw";
        c1<<"\n";
    }

    a1.close();
    b1.close();
    c1.close();
    return 0;
}

I'm writing to an external hard drive and using Windows 7.

Upvotes: 0

Views: 63

Answers (1)

Twifty
Twifty

Reputation: 3378

The simple answer is, your files are buffered into memory until the kernel (Windows in your case) decides it has nothing better to do and passes the buffer through to the driver.

This can be overcome by calling flush which will sync the buffer and the file, in effect, telling the kernel to write it straight away.

You can also tell ofstream to not use a buffer by calling setbuf(0,0). This will send all data directly to the file byte by byte. It will slow your program down considerably, and is only useful when the file is opened more than once at a time.

Upvotes: 1

Related Questions