0

I managed to compile the following main function using Windows Studio 2012 under a 64 bit release configuration: https://github.com/kpu/kenlm/blob/master/lm/builder/dump_counts_main.cc

Because I will be using it process TB of data I wanted to see if I could direct the output directly to a file instead of through cout. While I have found good advice on how to write files to disk fast I am not exactly sure how to pass the following statements into the fwrite functions.

std::cout << vocab.Lookup(*i) << ' ';
std::cout << *reinterpret_cast<const uint64_t*>(words + order) << '\n';

Anyone have any idea how they can be converted to an array of elements converted to a const void*?

Community
  • 1
  • 1
A Dark Divided Gem
  • 419
  • 1
  • 4
  • 18

1 Answers1

1

First off, to clear any misconceptions you might have:

  • fwrite is still (usually) buffered I/O (and that's a good thing)
  • ofstream is a perfectly good interface for doing formatted output to file
  • and ostream (and thus all of its derived classes) have the write member which serves the same purpose as fwrite.
  • and if you really insist on using cout for I/O, you can do ios_base::sync_with_stdio(false); at the beginning of your program, so that the iostream library doesn't have to bend over backwards to make writes to cout interoperate with stdio operations on STDOUT.

... but to answer the question you asked, you can just use a stringstream. You do formatted output to the string stream, then you extract the string it creates so that you can write its characters with fwrite.

  • Thanks, I am new to C++ so your answer provided some much needed context. My assumption is that its going to be faster to write direct to file via `ofstream` than piping the `cout` output into a text file from command prompt. – A Dark Divided Gem May 04 '15 at 00:39