gpt4 book ai didi

c - 程序在接受用户输入后不给出任何输出

转载 作者:行者123 更新时间:2023-11-30 17:12:03 25 4
gpt4 key购买 nike

我正在用 C 语言创建一个非常基本的程序,该程序将用户的单词作为输入并搜索它在文本文件中出现的次数并给出输出。代码是:

#include<stdio.h>
#include<string.h>
int main()
{
char user[20];
char word[20];
int i,pos=0,sum=0;
char c;
c='a';
printf("Enter the word you want to look for\n");
gets(user);
FILE *p;
p=fopen("D:\\trees.txt","r+");
do
{
pos=0;
fscanf(p,"%s",word);
if(c!=EOF)
{
if(strlen(word)==strlen(user))
{
for(i=0;i<strlen(user);i++)
{
if(word[i]==user[i]||word[i]==user[i]+32||word[i]==user[i]-32)
{
}
else
{
pos=1;
break;
}
}
}
else
{
pos=1;
}
if(pos=0)
{
sum++;
}
}
}
while(c!=EOF)

;printf("\nNumber of times %s appears is %d",user,sum);

fclose(p);
}

现在程序可以很好地接受输入,但不会给出任何输出。看起来像这样: this is the output window, which seems to be loading

我做错了什么?

最佳答案

看看评论,你的代码应该是这样的:

#include<stdio.h>
#include<string.h>
#include <ctype.h>
int main()
{
char user[20];
char word[20];
int n, pos=0, sum=0;
unsigned int i, l;
FILE *p;

do {
printf("Enter the word you want to look for\n");
} while (gets(user)==0);
user[strlen(user)-1]= '\0'; // remove trailing \n

if ((p=fopen("D:\\trees.txt","r+"))==0) {printf("Error opening file\n"); exit(0);}
do
{
pos=0;
n= fscanf(p,"%s",word);
if (n==1)
{
if(strlen(word)==(l=strlen(user)))
{
for(i=0; i<l; i++)
{
if(!(word[i]==user[i]||word[i]==tolower(user[i])||word[i]==toupper(user[i])))
{
pos=1;
break;
}
}
}
else pos=1;

if(pos==0) sum++;
}
}
while(n==1);

printf("\nNumber of times %s appears is %d",user,sum);

fclose(p);
return(1);
}

(进行了一些优化和添加)

关于c - 程序在接受用户输入后不给出任何输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31837609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com