代码在AS上运行的
异常
四种异常抛出方法
#include <iostream>#include <string>#include "mylog.h"#include "main_03.h"using namespace std;//异常测试void exceptionMethod01() {throw "我抛出了异常";}//异常测试2void exceptionMethod02() {throw exception();}//异常测试3 --自定义异常class CustomException : public exception {public:const char *what() const noexcept override {return "我要抛出自定义异常了";}};void exceptionMethod03() {throw CustomException();}//异常测试4 自定义异常class Student {public:char *getInfo(){return "自定义异常4";}};void exceptionMethod04(){Student s;throw s;}void main_03() {try {exceptionMethod01();} catch (const char *errMsg) {LOG("捕获到了异常信息:%s", errMsg);}try {exceptionMethod02();} catch (exception &errMsg) {LOG("捕获到了异常信息:%s", errMsg.what());}try {exceptionMethod03();} catch (CustomException &errMsg) {LOG("捕获到了异常信息:%s", errMsg.what());}try {exceptionMethod04();} catch (Student &errMsg) {LOG("捕获到了异常信息:%s", errMsg.getInfo());}}
IO流



IO缓冲区

cin.ignore(numeric_limits
文件操作


文件操作步骤

文件打开方式

文本文件的操作
char a;int index = 0;fstream fout;fout.open("testBuffer.txt", ios::app);if(fout.fail()){cout << "The file open fail" << endl;}while (cin >> a) {// cout << "in while:" << a << endl;fout << "in while: " << a << endl;index++;if (index == 5) {break;}}cin.ignore(numeric_limits<streamsize>::max(), '\n'); //清空缓冲区脏数据char ch;cin >> ch;// cout << "out while:" << ch << endl;fout << "out while:" << ch << endl;fout.close();
二进制文件的操作
#include <fstream>bool CopyFile(const string& str, const string& dst){ifstream in(str.c_str(), std::ios::in | std::ios::binary);ofstream out(dst.c_str(), std::ios::out | std::ios::binary | std::ios::trunc);if(!in || !out){return false;}char temp[bufferLen];while (!in.eof()){in.read(temp, bufferLen);streamsize count = in.gcount();out.write(temp, count);}in.close();out.close();return true;}
