来看看啊,我的程序为什么会重复输出?
当碰到(有=号,也有;号)的情况时,会输出两次那个句子,为什么?
#include <string.h>
#include <stdio.h>
#include <conio.h>
void main ()
{
FILE *funny,*fp;
char a[1001];
char line[1001],line3[1001];
char *pdest1,*pdest2;
funny=fopen("c:boot.ini","r");
fp=fopen("a.txt","w");
while (!feof(funny))
{
fgets(a,1000,funny);
if ((pdest2=strchr(a,';'))!=NULL) /*有;号*/
*pdest2='\0'; if (strchr(a,';')==NULL) /*没有;号*/
{
if((strchr(a,'[')!=NULL)&&(strchr(a,'=')==NULL))/*有[号,没有=号*/
{
strcpy(line,a);
if(strchr(line,']')!=NULL)
{
pdest1=strchr(line,']');
*(pdest1+1)='\n';
fputs(line,fp);
puts(line);
}
}
else if((strchr(a,'=')!=NULL)&&(strchr(a,'[')==NULL))
{
fputs(a,fp);
puts(a);
} else if((strchr(a,'[')!=NULL)&&(strchr(a,'=')!=NULL))
{
strcpy(line3,a);
fputs(line3,fp);
puts(line3);
}
}
};
fclose(funny);
fclose(fp);
getch ();
}
当碰到(有=号,也有;号)的情况时,会输出两次那个句子,为什么?
#include <string.h>
#include <stdio.h>
#include <conio.h>
void main ()
{
FILE *funny,*fp;
char a[1001];
char line[1001],line3[1001];
char *pdest1,*pdest2;
funny=fopen("c:boot.ini","r");
fp=fopen("a.txt","w");
while (!feof(funny))
{
fgets(a,1000,funny);
if ((pdest2=strchr(a,';'))!=NULL) /*有;号*/
*pdest2='\0'; if (strchr(a,';')==NULL) /*没有;号*/
{
if((strchr(a,'[')!=NULL)&&(strchr(a,'=')==NULL))/*有[号,没有=号*/
{
strcpy(line,a);
if(strchr(line,']')!=NULL)
{
pdest1=strchr(line,']');
*(pdest1+1)='\n';
fputs(line,fp);
puts(line);
}
}
else if((strchr(a,'=')!=NULL)&&(strchr(a,'[')==NULL))
{
fputs(a,fp);
puts(a);
} else if((strchr(a,'[')!=NULL)&&(strchr(a,'=')!=NULL))
{
strcpy(line3,a);
fputs(line3,fp);
puts(line3);
}
}
};
fclose(funny);
fclose(fp);
getch ();
}
fgets(a,1000,funny);要对a每次做初始化
你只要把程序改为: ......
fp=fopen("a.txt","w");//再这行下面加上:
fgets(a,1000,funny);
while (!feof(funny))
{
fgets(a,1000,funny);//把这一行注释掉 ......//你所作的处理不变 fgets(a,1000,funny);//在循环体的最后加上这句
}
fclose(funny);
fclose(fp);
getch ();
}不正确不给分.