Visual Studio中fstream指针的问题

时间:2016-03-09 14:51:14

标签: c++ visual-studio-2008

以下代码在使用mingw的代码块中工作正常,但在visual studio中失败。为什么?

fstream* __fstreamm = new fstream();
string _fileName="testing.log";
__fstreamm->open(_fileName.c_str(), std::fstream::in | std::fstream::out);
if(__fstreamm->fail())
{
    __fstreamm->open(_fileName.c_str(), std::fstream::trunc | std::fstream::out);
    __fstreamm->close();
    // re-open with original flags
    __fstreamm->open(_fileName.c_str(), std::fstream::in | std::fstream::out);
}
__fstreamm->seekp(0, ios::end);
*__fstreamm << "SSSSSSSSSTTTTTTTAAAAAAAAAARRRRRRRRRRRRTTTTTTTTTTT"<<std::endl;
__fstreamm->flush();

在使用代码块时创建并写入文件,但是在创建使用vs2008文件但未写入文件时。

1 个答案:

答案 0 :(得分:3)

我找到了解决方案。

fstream* __fstreamm = new fstream();
string _fileName="testing.log";
__fstreamm->open(_fileName.c_str(), std::fstream::in | std::fstream::out);
if(__fstreamm->fail())
{
    __fstreamm->open(_fileName.c_str(), std::fstream::trunc | std::fstream::out);
    __fstreamm->close();
    // re-open with original flags
    delete __fstreamm;
    __fstreamm = new fstream();
    __fstreamm->open(_fileName.c_str(), std::fstream::in | std::fstream::out);
}
__fstreamm->seekp(0, ios::end);
*__fstreamm << "SSSSSSSSSTTTTTTTAAAAAAAAAARRRRRRRRRRRRTTTTTTTTTTT"<<std::endl;
__fstreamm->flush();