C++ c++;txt文件不是由创建的。打开 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; int main() { 流文件; afile.open(“example.txt”); afile

C++ c++;txt文件不是由创建的。打开 #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; int main() { 流文件; afile.open(“example.txt”); afile,c++,C++,签出。如果文件不存在,我不相信fstream::open会创建该文件。您需要使用afile.open(“example.txt”,fstream::out);中提到的Tryafile.open(“example.txt”,std::ios::out)你读过关于给你带来麻烦的函数的文档了吗?哪些调用?你检查输入文件是否可以打开,但输出文件却不能。这是为什么?@RSahu的默认打开模式是ios_base::in | ios_base::out,所以out已经设置好了。@JoachimPilebor

签出。如果文件不存在,我不相信fstream::open会创建该文件。您需要使用
afile.open(“example.txt”,fstream::out);
中提到的
Try
afile.open(“example.txt”,std::ios::out)
你读过关于给你带来麻烦的函数的文档了吗?哪些调用?你检查输入文件是否可以打开,但输出文件却不能。这是为什么?@RSahu的默认打开模式是
ios_base::in | ios_base::out
,所以
out
已经设置好了。@JoachimPileborg:是的,但是.RSahu是正确的-。我使用过fstream::out也可以,但当我使用VS开始编译并在没有调试的情况下启动时,它不会创建文件。但是运行.exe文件会创建txt文件,但它仍然显示无法打开文件的输出
#include<conio.h>
#include<iomanip>
#include<cmath>
#include<iostream>
#include<fstream>
#include<string>
using namespace std;    
int main()
{
    fstream afile;
    afile.open("example.txt");
    afile<<"Hi I am Unnat";
    afile.close();
    string line;
    ifstream myfile ("example.txt");
    if (myfile.is_open())
    {
        while ( getline (myfile,line) )
        {
             cout << line << '\n';
        }
        myfile.close();
    }
    else cout << "Unable to open file"; 
    return 0;
}