【问题标题】:c++ txt file not created by .open [duplicate]c ++ txt文件不是由.open创建的[重复]
【发布时间】:2015-11-16 18:45:04
【问题描述】:
#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;
}

我在这里得到的输出总是无法打开文件,当我检查文件夹时我没有找到 example.txt 请帮忙。

【问题讨论】:

标签: c++


【解决方案1】:

查看C++ Reference。如果文件不存在,我不相信 fstream::open 会创建该文件。您需要使用afile.open("example.txt",fstream::out); 中提到的fstream won't create a file

【讨论】:

  • 我也使用过 fstream::out 但是当我用 VS 开始编译并且没有调试就开始时,它不会创建文件。但是运行 .exe 文件会创建 txt 文件,但它仍然显示输出 Unable to open file
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
相关资源
最近更新 更多