将文本写入文件中

Write a text into file

本文关键字:文件 文本      更新时间:2023-10-16

我刚刚在Visual Studio 2017中启动了新项目,我试图将一些文本写入文件中。但是在运行代码后,没有创建文件。

int main()
{
    ofstream file_program("D:test2.txt", ios::out);
    if (file_program.is_open())
    {
        file_program << "test";
        cout << "OK";
        file_program.close();
    }
    int age;
    cin >> age;
    return 0;
}

我在做什么错?

在C 中,您必须逃脱后背。

"D:test2.txt"

在这里,您有't'实际上是制表符。

"D:\test2.txt"

是正确的。