LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 697|回复: 2

美化代码

[复制链接]
发表于 2003-4-24 22:03:12 | 显示全部楼层 |阅读模式
BBS上粘贴代码常造成缩进丢失,这里提供一个简单的解决方法。
下面为源文件fmtc.c,
gcc -o fmtc fmtc.c


//begin  fmtc.c
/*===========================================
这个程序用来简单美化c代码
适用于BBS中粘贴代码造成无缩进格式的文件
用法:
1、fmtc 源文件 目标文件

2、fmtc 源文件

用1法时,保留源文件,美化生成目标文件
用2法时,直接将源文件美化保存
=============================================*/

#include <unistd.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
        FILE *fp_sourcefile, *fp_destinefile;
        char c_getchar;
        char *cp_tmpfilename;
        int i_count, i;
        cp_tmpfilename="tmp";

        i_count=0;

        if(argc<2)
        {
                printf("usage: fmtc sourcefile destinefile\n");
                printf("or  fmtc sourcefile\n");
                return ;
        }

        fp_sourcefile=fopen(argv[1],"r");
        if(!fp_sourcefile)
        {
                printf("%s open error!\n",argv[1]);
                return ;
        }

        if(argc==3)
                cp_tmpfilename=argv[2];

        fp_destinefile=fopen(cp_tmpfilename,"w");

        if(!fp_destinefile)
        {
                printf("file can not Create!\n");
                return ;
        }

        while((c_getchar=fgetc(fp_sourcefile))!=EOF)
        {
                fputc(c_getchar,fp_destinefile);

                if(c_getchar=='\n')
                {
                        while((c_getchar=fgetc(fp_sourcefile))!=EOF)
                        {
                                if(c_getchar==' '||c_getchar=='\t')
                                        continue;
                                else
                                {
                                        if(c_getchar=='}')
                                        {
                                                i_count--;
                                                printf("i_count=%d\n",i_count);
                                        }
                                        for(i=0; i<i_count; i++)
                                        {
                                                fputc('\t',fp_destinefile);
                                                printf("tab%d\n",i);
                                        }
                                        if(c_getchar=='{')
                                        {
                                                i_count++;
                                                printf("i_count=%d\n",i_count);
                                        }
                                        fputc(c_getchar,fp_destinefile);
                                        break;
                                        fputc(c_getchar,fp_destinefile);
                                        break;
                                }
                        }
                }
        }
        fclose(fp_sourcefile);
        fclose(fp_destinefile);
        if(argc==2)
        {
                fp_sourcefile=fopen(argv[1],"w");
                if(!fp_sourcefile)
                {
                        printf("%s open error!\n",argv[1]);
                        return ;
                }

                fp_destinefile=fopen(cp_tmpfilename,"r");
                if(!fp_destinefile)
                {
                        printf("file can not Create!\n");
                        return ;
                }

                while((c_getchar=fgetc(fp_destinefile))!=EOF)
                {
                        fputc(c_getchar,fp_sourcefile);
                }
                fclose(fp_sourcefile);
                fclose(fp_destinefile);
                unlink(cp_tmpfilename);
                return;
        }
}
//end program

大家可用本贴试试
自由狼-台风 该用户已被删除
发表于 2003-4-24 22:12:24 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2003-4-24 22:38:39 | 显示全部楼层
太好了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表