简体   繁体   English

程序在接受用户输入后未提供任何输出

[英]Program not giving any output after taking input from user

I was creating on a very basic program in C which takes a word from user as input and searches for how many times it appears in a text file and gives output. 我正在C语言中创建一个非常基本的程序,该程序将用户输入的单词作为输入,并搜索它在文本文件中出现的次数并给出输出。 The code is: 代码是:

#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);
}

Now the program takes the input fine, but doesn't give any output. 现在,程序可以很好地处理输入,但不提供任何输出。 Looks like this: 看起来像这样: 这是输出窗口,似乎正在加载

What have I done wrong? 我做错了什么?

Looking at the comments, your code should be something like: 查看注释,您的代码应类似于:

#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);
}

(with some optimizations and additions) (进行一些优化和添加)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM