Multiple output pointer to a file in c++

c++ output fstream

52 观看

3回复

3 作者的声誉

I want two pointers to a same file in C++.

#include <fstream>

int main()
{
    double a;
    std::ofstream fout1( "out.txt");
    fout1 << "CASE A:"<<std::endl;

    std::ofstream fout2( "out.txt", std::fstream::app);
    fout2 << "CASE B:" <<std::endl;

    for (int i=0; i < 3; i++){
        a = i*i;
        fout1 << a << std::endl;
        fout2 << 10+a <<std::endl;
    }
    return 0;
}

This code give me:

CASE A:
0
1
4


11
14

But I wanna:

CASE A:
0
1
4
CASE B:
10
11
14

In my real code variable "a" is a huge calculation, so do a double for would not be a good solution. I thought of 2 solutions:

  1. Using double for saving "a" in a vector;
  2. Using two pointer to same file (current question);

I think 2 is the best, so how can I use two pointer to the same file?

作者: Bismarck Gomes 的来源 发布者: 2017 年 12 月 27 日

回应 3


0

7904 作者的声誉

Using two streams to write to the same file is going to turn into a nightmare, you have to flush and resync wherever one stream changes in order to ensure that the other sees those changes. Much better to do away with this idea and approach the problem using a single output.

If you are going to stick with the idea, each time you are going to switch to a different stream call:

firstStrm.flush();
secondStrm.tie(firstStrm);
secondStrm.seekp(firstStrm.tellp())

where firstStrm is the stream you are done writing with and secondStrm is the stream you want to start writing with.

作者: SoronelHaetir 发布者: 2017 年 12 月 27 日

0

22159 作者的声誉

The fundamental problem you have is that writes to fout1 clobber previous writes to fout2. Since you don't know where in the file you need to start writing out your "CASE B" data, you have to wait until all of the "CASE A" data is written before you start with "CASE B".

You can either write the CASE B data to a different (temporary) file, then copy that data over when you're done with CASE A (and delete the temporary file), accumulate the data you would write into a string buffer, then write that out.

Or do your choice 1, which is to save all the a values into a vector, then process those at the end. This is likely the fastest way, although if you have large amounts of data memory could be a concern and you'd have to go with the two file approach.

作者: 1201ProgramAlarm 发布者: 2017 年 12 月 27 日

0

45597 作者的声誉

If you want two pointers to the same file, then create them:

std::ofstream  fout("out.txt");
std::ofstream * fout1(&fout);
std::ofstream * fout2(&fout);

The problem with having more than one pointer to an output stream is you can have data output in a random order, depending on the execution. This is worse with multiple threads or multiple tasks.

Another solution is to pass the output stream, by reference, to constructors or functions. A class can have a copy of the stream:

class Hello
{
  public:  
    Hello(std::ostream& output)
    : m_output(output)
    { }
  private:
    std::ostream& m_output;
};
作者: Thomas Matthews 发布者: 2017 年 12 月 27 日
32x32