⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 12_3.cpp

📁 老师上机常用的VC++ 6.0的相关例题
💻 CPP
字号:
//12_3.cpp
#include<iostream>
#include<fstream> //包含文件流头文件
#include"employee.h"
using namespace std;

class FileException {   //文件异常处理类
public:
   FileException() 
      : message( "File is not created!" ) { }
   const char *what() const { return message; }
private:
   const char *message;
};

int main()
{
	ifstream infile("employee.txt",ios::in);	//创建一个输入文件流对象
	try {  
		if (!infile)
		  throw FileException();  //抛出异常
    }
    catch ( FileException fe ) // 捕捉异常
{ 
       cout<<fe.what()<<endl;  //输出异常
	    exit(0);   //退出程序
	 }
	cout<<"从文件中读取信息并显示如下:"<<endl;
	//从文件读入人员信息
	char line[101];
	for(int i=0;i<4;i++)
	{
	   infile.getline(line,100);
	   cout<<line<<endl;
	}
	infile.close();
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -