C语言 编写一个程序 从该文件中查找某个字符(该字符可以输入),如果查找成功

2024-12-05 00:19:25
推荐回答(1个)
回答(1):

#include
#include
void Input()
{
FILE *fp;
if((fp = fopen("file1.txt","w")) ==NULL)
{
exit(1);
}

char a;
printf("输入一串字符,以$结束\n");
while(scanf("%c",&a) && a!= '$')
{
fputc(a,fp);
}
fflush(stdin);
fclose(fp);
}
int Find(char c)
{
FILE *fp;
int flag = 0;
if((fp = fopen("file1.txt","r")) ==NULL)
{
exit(1);
}
while(!feof(fp))
{
char ch = fgetc(fp);
if(ch == c)
flag = 1;
}
if(flag == 1)
{
printf("find!\n");
}
else
printf("not find!\n");
fclose(fp);
return 0;
}
int main()
{
char cha;
Input();
printf("输入要查找的字符:");
scanf("%c",&cha);
Find(cha);
return 0;
}
看看是不是你要的