1

Hopefully I haven't missed an already existing answer to this. obs! i'm not looking for help on formatting as in this question How do I correctly organize output into columns?

I'm generating multiple columns of data, one column at a time. Each column is generated in one step of a for-loop, and I'm using std::ofstream write everything to textfiles while creating the values.

Now to my question, can I write each of these columns, while creating them, to the same stream?

I will try to clarify with an example: Lets say the final data looks like this:

   Column1  Column2  Column3
   val11    val12    val13
   val21    val22    val23
   val31    val32    val33

but the data is generated in this fashion:

    for-loop step 1: val11 val21 val31
    for-loop step 2: val12 val22 val32
    for-loop step 3: val13 val23 val33

I would like to write to the stream like this (inside the for-loop)

    stream step 1: val11 empty empty\n  val21 empty empty\n  val31 empty empty\n
    stream step 2: val11 val12 empty\n  val21 val22 empty\n  val31 val32 empty\n
    stream step 3: val11 val12 val13\n  val21 val22 val32\n  val31 val32 val33\n

What I try to obtain can naturally be done by e.g. storing each column in a std::vector or similar and then looping to print to stream. However, in my case each column will have n_choose_k values, and I would like my application to not to go out of memory too fast.

The third option is of course to store each column in some buffer file or separate ofstream to offset the RAM and then do some kind of merge in the end.

Regarding potential pre-allocation, I know beforehand the number of elements in each column.

Community
  • 1
  • 1
Schaki
  • 1,697
  • 1
  • 12
  • 14
  • 1
    If the values are all fixed size, then just output that many spaces for `empty`, then use `fseek` to get back to those spaces (they will be at predictable offsets) and overwrite them with actual data as it becomes available. If the values are not fixed size, then I'm not sure what you mean by "preallocating". – Igor Tandetnik Jun 05 '14 at 13:17
  • Thanks! `fseek` might be the solution then, my hope was that there would be some smooth library package tailored for storing data columnwise or similarly. – Schaki Jun 05 '14 at 22:28

0 Answers0