basic_filebuf。输出文件中有很多奇怪的文字

时间:2012-04-18 07:01:47

标签: c++ std

1)我有这段代码

//... many code below
      std::basic_filebuf<char, std::char_traits<char> > streamIn;
      streamIn.open("file.txt", std::ios_base::trunc | std::ios_base::out);
      streamIn.sputn("Hello", 5);
//...

但是在file.txt中我看到很多奇怪的文字。你好。这个文件还包含了我在DB的所有记录!我不知道为什么

2)我可以在没有文件的情况下使用 std :: basic_filebuf 吗?喜欢 std :: basic_stringbuf

找到解决方案1)

// ...
    std::basic_filebuf<char, std::char_traits<char> > streamIn;
    streamIn.open("file.txt", std::ios_base::out);
    streamIn.sputn(responce.c_str(), responce.size());
    streamIn.close();

    streamIn.open("file.txt", std::ios_base::in);
//...

1 个答案:

答案 0 :(得分:0)

如上所述here,basic_filebuf是一个与文件交互的basic_streambuf。

所以不,我不认为你可以在没有文件的情况下使用它。

您可能想要做的是改为使用basic_streambuf,并控制您是希望它是文件(使用basic_filebuf)还是字符串(使用basic_stringbuf)。

相关问题