Linux之C编程错误处理、多文件工程、man的使用技巧

一、多文件工程----Qt下
1、如果将windows下的 cpp和h文件,导入到Linux下的Qt4工程中:
步骤:
1)先将cpp和h文件 拷贝到VMWare的共享文件夹(如 sharefiles)下
2)在Linux的QtCreator中,建立 空工程
3)在Linux的图形界面下,将/mnt/hgfs/sharefiles下的 cpp和h文件,拷贝到 上一步的工程文件夹下
4)使用Linux shelll,进入到工程文件目录下,执行
dos2unix *.cpp
dos2unix *.h
注意:应用提示信息,进行了个数转换
5)在Linux的QtCreator中,在项目上点右键----Add Exiting Files------选取cpp和h文件
至此,导入完成


因为 Linux下没有 "cls"  "pause"命令,因此需要修改相关代码,代码如下
代码:
#include <curses.h>
#include <stdio.h>
#include <iostream>


#include "Manage.h"
using namespace std;


int main()
{
    CManage manage;
    while(1)
    {
        cout<<"1:请输入学生信息"<<endl;
        cout<<"2:请输入教师信息"<<endl;
        cout<<"3:打印所有学生信息"<<endl;
        cout<<"4:打印所有教师信息"<<endl;
        cout<<"0:退出"<<endl;
        char flag;
        cin>>flag;
        switch (flag)
        {
        case '1':
            cout<<endl;
            //cout<<"输入学生信息"<<endl;
            manage.student_add();
            break;
        case '2':
            cout<<endl;
            cout<<"输入教师信息"<<endl;

            break;
        case '3':
            cout<<endl;
            //cout<<"所有学生信息"<<endl;
            manage.print_student_all();
            break;
        case '4':
            cout<<endl;
            cout<<"所有教师信息"<<endl;
            break;
        case '0':
            cout<<endl;
            cout<<"退出"<<endl;
            return 1;
        default:
            cout<<endl;
            cout<<"输入错误,请重新输入!"<<endl;
            break;
        }
        cout<<"按任意键继续"<<endl;
        getchar();   //将输入缓存中的字符 '\n'扔掉
        getchar();   //等待用户从键盘输入字符,并按回车
        system("reset");  //reset为shell下的 复位清屏 命令
    }
    return 0;

}


二、C编程中的错误处理-----errno  perror   strerror
errno          ----错误号。注意:系统函数调用后,立即检查;如何中间隔了其它函数,可能就失效了。
perror         ----函数,用法见下文  
strerror    ----函数,用法见下文 

代码:
#include <stdio.h>
#include <errno.h>

FILE *fp=fopoen("a,txt","r");
perror("open a.txt failure");

说明:
    当没有a.txt文件时,fopen返回NULL,即fp被赋值为NULL
    同时,fopen函数还会修改errno----错误号
    所以,通过errno错误号,可以得到更详细的 错误信息。
    而perror就是用来根据 错误号,打印 其对应的字符串内容
    本例中,会输出
    open a.txt failure:No such file or directory
    
    
代码:
    
#include <stdio.h>
#include <string.h>
#include <errno.h>

FILE *fp=fopoen("a,txt","r");
printf("%s\n",strerror(errno));        //strerror(errno)将返回1个char *字符串,它代表了errno错误号的字符串信息  需要string.h头文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_38220914

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值