2

Boostライブラリのみを使用してテキストをファイルに出力できますか?

私が持っているコード(公式ドキュメント):

#include <ostream>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/stream.hpp>

namespace io = boost::iostreams;

int main()
{
    io::stream_buffer<io::file_sink> buf("log.txt");
    std::ostream                     out(&buf);
    // out writes to log.txt
    out << "abc";
}

別の方法はありますか?(私は標準ストリームを使いたくありません)。前もって感謝します。

4

2 に答える 2

7

Input and output in a C++ program can be done in four ways:

  1. Using C++ I/O streams. This is the recommended way. Even if using Boost I/O streams, it's all using the standard I/O streams behind the scene, and using the stream operators << and >>.
  2. The C FILE API: fopen, fprintf/fwrite/fgets/fread/etc. I think there are Boost I/O streams that handle FILE, but then you are still using the stream operators << and >>.
  3. On Windows, use of the POSIX subsystem file descriptors. I think these can also be used for Boost I/O streams.
  4. Native file handling. On POSIX systems (e.g. Linux, BSD, OSX) this is the same as 3 above, on Windows see e.g. this link.

Boost I/O streams isn't supposed to be a stand-alone API, instead it is using one of the above I/O systems to simplify some things for the programmer.

于 2012-07-24T06:59:03.363 に答える
2

io::stream_bufferは から派生してstd::basic_streambufいるため、ネイティブのbasic_streambufパブリック メソッドを使用したり、boost::filesystemストリームを使用したりできますが、そのようなクラスはすべてクラスから派生してstd::います。

http://www.boost.org/doc/libs/1_50_0/libs/filesystem/doc/reference.html#ファイルストリーム

于 2012-07-24T06:48:05.287 に答える