经常在网上看到代码,复制过来后老是发现左侧有多余的空格,看着很不爽,就像下面这样....
所以就想写个小程序修改一下,下面是代码:
#include<stdio.h>
#include<string.h>
#define ORIGINAL_NAME "snake.c" //original file name
#define NEW_NAME "result.c" //new file name
#define SPACES 4 //numbers of spaces
#define BUF_SIZE 100
int main()
{
FILE * origin_file, * new_file;
char buf[BUF_SIZE];
int flag=1;
int len;
if((origin_file=fopen(ORIGINAL_NAME,"r"))==NULL || (new_file=fopen(NEW_NAME,"w"))==NULL)
{
printf("can't open the file!\n");
}
while(fgets(buf,BUF_SIZE,origin_file)!=NULL)
{
len=strlen(buf);
if(1==flag)
{
if(len==1)
{
fputs(buf,new_file);
}
else
{
fputs(buf+SPACES,new_file);
}
if(buf[len-1]!='\n')
{
flag=0;
}
}
else
{
fputs(buf,new_file);
if(buf[len-1]=='\n')
{
flag=1;
}
}
}
fclose(origin_file);
fclose(new_file);
return 0;
}
修改之后的文档就变成了:
![](https://img-my.csdn.net/uploads/201207/13/1342169220_7770.png)
看起来比以前好多了...