#include
#include
#include
#include
#define MAXLINELENGTH 1024
//定义解析字符状态
enum CHARACTERTYPE
{
INIT,
SPACE,
TAB,
PREVTAB,
PREVSPACE,
OTHER,
END
};
int main (void)
{
FILE *fpin = NULL;//输入文件指针
FILE *fpout = NULL;//输出文件指针
enum CHARACTERTYPE currchartype = INIT;//当前字符解析状态
enum CHARACTERTYPE prevchartype = INIT;//上一字符解析状态
char inputbuffer[MAXLINELENGTH];//输入文件行缓冲区
char outputbuffer[MAXLINELENGTH];//输出文件行缓冲区
char *ptrch = NULL;
int i;
if ((fpin = fopen("fcopy.in","r")) == NULL) //打开输入文件
{
fprintf(stderr,"To open input file fcopy.in failed[%s]\n",strerror(errno));
exit(-1);
}
if ((fpout = fopen("fcopy.out","a")) == NULL) //打开输出文件
{
fprintf(stderr,"To open output file fcopy.out failed[%s]\n",strerror(errno));
exit(-1);
}
do
{
memset(inputbuffer,0,sizeof(inputbuffer));
memset(outputbuffer,0,sizeof(outputbuffer));
i = 0;
ptrch = fgets(inputbuffer,MAXLINELENGTH,fpin);//逐行读取输入文件
if (ptrch == NULL)
{
if (errno > 0)
{
fprintf(stderr,"To read fcopy.in line by line failed[%s]\n",strerror(errno));
exit(-1);
}
else//文件结束
{
currchartype = END;
}
}
else
{
if (inputbuffer[0] == '\n')//行首是回车符直接读下一行
{
currchartype = INIT;
prevchartype = INIT;
continue;
}
while(*ptrch != '\n' && *ptrch != EOF)//当未读到回车或文件结束进入状态机处理
{
prevchartype = currchartype;
if (*ptrch == ' ')//空格字符处理
{
if (prevchartype == SPACE || prevchartype == TAB)
{
currchartype = PREVSPACE;
ptrch++;
continue;
}
else
{
currchartype = SPACE;
}
}
else if (*ptrch == '\t')//制表符处理
{
if (prevchartype == OTHER)
{
currchartype = PREVTAB;
}
else if (prevchartype == SPACE || prevchartype == TAB)
{
currchartype = TAB;
}
}
else//其他字符处理
{
if (prevchartype == PREVTAB || prevchartype == TAB
|| prevchartype == PREVSPACE || prevchartype == SPACE)
{
outputbuffer[i] = ' ';
i++;
}
currchartype = OTHER;
outputbuffer[i] = *ptrch;
i++;
}
ptrch++;
}/*End of while loop*/
outputbuffer[i] = '\n';
if (fputs(outputbuffer,fpout) < 0)//输出一行
{
fprintf(stderr,"To write fcopy.out failed[%s]\n",strerror(errno));
exit(-1);
}
}
} while(currchartype != END);
fclose(fpin);
fclose(fpout);
return 0;
}
测试输入 haha hello world ni hao!
测试输出 haha hello world ni hao!
#include
int main()
{
FILE *a,*b;
int c, c1=0;
a=fopen("fcopy.in", "rb");
b=fopen("fcopy.out", "wb");
while((c=fgetc(a)) !=EOF)
{
if(c == ' ' || c=='\t')
{
if(c1 == ' ' || c1=='\t')
continue;
}
fputc(c, b);
c1=c;
}
fcloseall();
return 0;
}
#include
int main()
{
char ch;
int flag=1;
FILE *fin,*fout;
fin=fopen("fcopy.in","r");
fout=fopen("fcopy.out","w");
while((ch=fgetc(fin))!=EOF)
{
if(flag)
{
fputc(ch,fout);
}
if(ch=='\40'||ch=='\t')
{
flag=0;
}
else
{
flag=1;
}
}
fclose(fin);
fclose(fout);
return 0;
}
直接拿源代码无疑是饮鸩止渴