文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C语言实例61:错误处理

C语言实例61:错误处理

时间:2010-07-04  来源:hnrainll

#include <stdio.h>
#include <stdlib.h>
#define TAB_NUM 8
#define IN 0
#define OUT 1

void error(int e)
{
    if(e == IN)
        printf("输入错误.\n");
    else
        printf("输出错误.\n");
    exit(1);
}



int main(int argc, char *argv[])
{
    FILE *in, *out;
    int tab, i;
    char ch;

    if(argc != 3)
    {
        printf("用法出错。\n");
        exit(1);

    }

    if((out = fopen(argv[1], "wb")) == NULL)
    {
        printf("不能打开输入文件%s.\n",argv[1]);
        exit(1);
    }

    if((out = fopen(argv[2], "wb")) == NULL)
    {
        printf("不能打开输出文件%s.\n",argv[2]);
        exit(1);
    }
    tab = 0;
    do{
        ch = getc(in);
        if(ferror(in))
            error(IN);

        if(ch == '\t')
        {
            for(i = tab; i < 8; i++)
            {
                putc(' ', out);
                if(ferror(out))
                    error(OUT);
            }
            tab = 0;

        }
        else
        {
            putc(ch, out);
            if(ferror(out))
                error(OUT);
            tab++;
            if(tab == TAB_NUM)
                tab = 0;
            if(ch == '\n' || ch == '\r')
                tab = 0;
        }
    }while(!feof(in));

    fclose(in);
    fclose(out);

    system("pause");
    return 0;
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载