Writer私は2人のメンバーを持つクラスを持っていofstreamます。両方のストリームが同じ出力ファイル
に関連付けられています。メソッドで両方のストリームを使用したいのですが、各ストリームが実際の出力ファイルの最後に書き込むようにします。Writer::write
コード
class my_ofstream1:
public ofstream
{
// implement some functions.
// using internal, extended type of streambuf
};
class my_ofstream2:
public ofstream
{
// implement some functions.
// using internal, extended type of streambuf
// (not the same type as found in my_ofstream1)
};
class Writer
{
public:
void open(string path)
{
f1.open(path.c_str(),ios_base::out); f2.open(path.c_str(),ios_base::out);
}
void close()
{
f1.close(); f2.close();
}
void write()
{
string s1 = "some string 1";
string s2 = "some string 2";
f1.write(s1.c_str(), s1.size());
// TBD - ensure stream f2 writes to the end of the actual output file
assert(f1.tellp() == f2.tellp());
f2.write(s2.c_str(), s2.size());
}
private:
my_ofstream1 f1;
my_ofstream1 f2;
};
void main()
{
Writer w;
w.open("some_file.txt");
w.write();
w.close();
}
質問と同期している
ことを確認するにはどうすればよいですか? つまり、書き込む前に、 のストリーム オフセットは のストリーム オフセットと同期している必要があります。それぞれが特別な派生を使用する
関数std::ios::rdbufを使用できません。使用すると、必要な機能が失われます。Synchronizing Streamsトピック
にあるいくつかの手法を使用してみましたが、実現できませんでした。f2f1f2f1ofstreamstreambufrdbuf()