|
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
大家可用本贴试试 |
|