为什么文件没有打开C++?

Afz*_*han -5 c++ fstream

我只是第一次尝试这个代码.我无法找到错误的根源.

这是代码:

#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    fstream file;
    file.open("C:\\Users\\AfzaalAhmad\\Documents\\text.txt");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该文件存在于该位置.这是文件系统的屏幕截图.

文件系统的屏幕截图

在这种情况下没有例外,但文件永远不会打开!

我在哪里丢失代码?

chr*_*244 5

您编写的命令将打开该位置文件的句柄.为了对它做任何事情,你需要某种读或写操作.可能你的代码工作得很好:)

例如,按照您的file.open("...")行:

file << "This is some text to send to my now open file\n";
...
file.close();
Run Code Online (Sandbox Code Playgroud)