c# - Generating a .txt File From a DataTable -


i creating text file on fly based on data gathered in datatable.

my current (test) dataset has 773 rows, want split comma (,) each column , break each row on separate line. here's attempt;

string filename = "test_" + system.datetime.now.tostring("ddmmyyhhmm") + ".txt";                  streamwriter sw = file.createtext(@"path...." + filename);                  foreach (datarow row in product.rows)                 {                     bool firstcol = true;                     foreach (datacolumn col in product.columns)                     {                         if (!firstcol) sw.write(",");                         sw.write(row[col].tostring());                         firstcol = false;                     }                     sw.writeline();                 } 

the output text file, expected. instantly, of data appears, text file never displays 773 rows. have tried several times, number of rows can vary 720 rows 750 rows way partial completion of row 773, never finishes.

i haven't interfered with, or stopped application @ time.

any ideas?

short answer

you need flush stream, either wrapping streamwriter in using block, or call sw.flush() @ end of foreach loop.


long answer

when using streamwriter of kind (streamwriter, binarywriter, textwriter), writing underlying stream/device (in case file) - not write directly file, expensive, compared using buffer.

(1) imagine following:

  • you're looping through 10.000 records
  • each record written file directly when invoking .write(), before moving on next record.

(2) how works:

  • you're looping through 10.000 records
  • each record written buffer when invoking .write(), before moving on next record.
  • when buffer reaches size/number of elements flush disc.

you can see using (2) gain lot of io performance compared (1), each new record doesn't need written file immediately.

so (1) need write file 10.000 times, while (2) has write fraction of (1) needs (which 5.000 times, 2.000 times - depends on how buffer implemented).

by wrapping stream in using block, or calling flush() on it, whenever you're done stream make flush buffer (and missing data) file.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -