版权声明:转载商用请通知本人 https://blog.csdn.net/qq_41076577/article/details/84494038
定义输出流对象fout,先打开文件out.txt,再判断是否成功打开,输出Hello World。代码如下:
#include<iostream>
#include<fstream>
using namespace std;
int main(){
ofstream fout;
fout.open("out.txt"); //打开文件
if(fout.fail()) //如果打开失败
cout<<"out.txt 文件打开失败"<<endl;
else
fout<<"Hello World"<<'\n'; //输出
}