C Primer Plus书中代码注释-chapter17_16_fileio.cpp

chapter17_16_fileio.cpp

#include <iostream>
#include <fstream>
#include <string>

int main(){
	
	using namespace std;
	string filename;
	
	cout << "Enter name for new file: ";
	cin >> filename;
	
	ofstream fout(filename.c_str());		//string类型的字符串用作文件名时,需要转换为C-风格的字符串提供给构造函数ofstream和ifstream
	
	fout << "For your eyes only!\n";		//向新建的文件中输入
	cout << "Enter your secret number: ";
	float secret;
	cin >> secret;
	fout << "Your secret number is " << secret << endl;	//向新建的文件输入
	fout.close();			//关闭输入流,并没有删除流,只是断开流到文件的连接。流管理装置仍被保留
	
	ifstream fin(filename.c_str());
	cout << "Here are the contents of " << filename << ":\n";
	char ch;
	while(fin.get(ch))
		cout << ch;
	cout << "Done\n";
	fin.close();
	
	return 0;
}
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页