Hi everyone,
I'm wondering if anyone knows the optimal way to write a very big tab-sepparated file with data that consists mostly of ints and floats. This ints and floats are really rows of data that will be input in a database. What I'm trying to do is to use a string to save like 500 rows and then I write to disk. It's rather slow and takes like 1.18 minutes to write all of this to a file. Anyone know a way to optimize better?
Code:
std::string to_file = "";
for (int i=0; i<numRecords; ++i) {
for (ssize_t fldidx = 0; fldidx < hdr.Length(); ++fldidx) {
switch (field_type) {
case UNDEFINED:
continue;
case STRING:
continue;
case CHAR: {
continue;
}
case INT: {
const int field_addr = field.mNumVal.i;
char result[100];
sprintf( result, "%d", field_addr );
to_file += result;
break;
}
case FLOAT: {
const float field_addr = field.mNumVal.f;
char result[100];
sprintf( result, "%f", field_addr );
to_file += result;
break;
}
case DOUBLE: {
const double field_addr = field.mNumVal.d;
char result[100];
sprintf( result, "%f", field_addr );
to_file += result;
break;
}
case LONG: {
const long field_addr = field.mNumVal.l;
char result[100];
sprintf( result, "%d", field_addr );
to_file += result;
break;
}
}
if (fldidx != hdr.Length()-1)
to_file += "\t";
else
to_file += "\n";
}
if ( (i % bulk == 0) || i == (numRecords-1) ) {
fout << to_file;
to_file = "";
}