首页 文章

如果不存在利用seekp / seekg,使用fstream创建文件?

提问于
浏览
-1
fstream file(file_1.c_str(), ios::out | ios::in | ios::ate); //convert string to const char*

如果文件不存在则出错 . if ( ! file ) = true

fstream file(file_1.c_str(), ios::out | ios::in | ios::app); //convert string to const char*

使用 ios::app seekgseekp 函数不起作用 . file.seekg(4, ios_base::beg);

我想拥有:

  • USER输入文件名

  • 如果NON存在则创建文件

  • 使用seekg&seekp;

  • 如果再次运行程序,则不删除该文件 .

1 回答

  • 0

    如果我理解正确,你需要这样的东西:

    #include <iostream>
    #include <fstream>
    #include <string>
    
    int main()
    {
        string name;
        std::cin>>name;
        std::ofstream file (name.c_str());
        outfile << "Text in file." << std::endl;
        file.close();
        return 0;
    }
    

相关问题