C++学习笔记——文本文件的读写

#include<iostream>
#include<fstream>
using namespace std;
int main(){
    
    
	//打开文件
	ofstream out("test.txt");
	if(!out){
    
    
		cout<<"打开文件失败"<<endl;
		return 1;
	} 

	//写文件
	out<<"欢 迎 来 到 北 京!";
	out.close();//关闭文件 

	//打开文件
	ifstream in("test.txt");
	if(!in){
    
    
		cout<<"打开文件失败"<<endl;
		return 1;
	}

	//读文件 
	while(in){
    
    
		char c = in.get();
		cout<<c;
	}
	in.close();//关闭文件 

	return 0;
}

运行结果:欢 迎 来 到 北 京!

猜你喜欢

转载自blog.csdn.net/wxsy024680/article/details/113729131