linux C++之错误处理

来源:互联网 发布:苹果下歌用什么软件 编辑:程序博客网 时间:2024/05/24 07:13
/* * main.cpp * *  Created on: Jul 5, 2014 *      Author: john */#include<iostream>#include<string.h>#include<errno.h>#include<fstream>using namespace std;int main(){   ofstream sampleout;   sampleout.open("1",ios::app);   if(!sampleout)   {   cout<<"shibai\r\n";   cout<<strerror(errno)<<endl;//strerror函数需要头文件string.h   return 0;   }   sampleout.write("hehehe",strlen("hehehe"));   sampleout.close();   ifstream samplein;   samplein.open("1");   if(!samplein)   {   cout<<strerror(errno)<<endl;//strerror函数需要头文件string.h   }   char inBuf[256]={0};   int nCount=0;   while(samplein.getline(inBuf,256))   {       cout<<inBuf<<endl;       cout<<nCount++<<endl;       memset(inBuf,0,256);   }   samplein.close();}

其实这一部分,最主要的就是strerror函数,该函数声明在string.h当中,能够根据传入的错误代码,返回我们能够理解的错误信息
0 0