Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1598601
  • 博文数量: 354
  • 博客积分: 8137
  • 博客等级: 中将
  • 技术积分: 5137
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-26 15:40
文章分类

全部博文(354)

文章存档

2010年(300)

2009年(54)

分类: C/C++

2010-07-04 12:49:35

#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;
}


阅读(475) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~