首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在文件中使用puts

在文件中使用puts
EN

Stack Overflow用户
提问于 2014-02-05 07:45:06
回答 2查看 69关注 0票数 0

因此,我有一些代码可以将文件中的字符串与用户输入进行比较,该输入已经过错误检查,以删除空格。

代码语言:javascript
复制
char text[100];
int c = 0;
int d = 0;
char string[100];

void space(void);

int main(/*int argc,char *argv[]*/)
{
  int count = 1;
  char wd[20], word[20];
  FILE *fp;

  fp = fopen("Student Usernames.txt", "r");
  if (fp == NULL)
  {
    printf("given file doesn't exist");
    getch();
  } else
  {
    printf("Enter the word to search: ");
    gets(text);
    space();

    fscanf(fp, "%s", wd);
    while (!feof(fp))
    {
      if (strcmp(string, wd) == 0)
      {
        printf("%s found in the file. the given word is the %d word in the file",
                word, count);
        count = 0;
        break;
      } else
      {
        fscanf(fp, "%s", wd);
        count++;
      }
    }
    if (count != 0)
    {
      printf("given word is not found in the file");
    }
    getch();
  }
}

void space(void) //Prototype stage, w/o semi colon
{
  while (text[c] != '\0')
  {
    if (!(text[c] == ' ' && text[c] == ' '))
    {
      string[d] = text[c];
      d++;
    }
    c++;
  }
  string[d] = '\0';
  c = 0;
  d = 0;

  getch();
}

然而,有一个小问题,当我继续显示代码时,它会给出一些随机垃圾作为文件中的单词,

例如:输入:A p p l e。程序将A p p l e变成Apple。输出:%^&$£$%^&

有人知道哪里出问题了吗?编辑:添加文本。

EN

回答 2

Stack Overflow用户

发布于 2014-02-05 12:19:23

代码语言:javascript
复制
    printf("%s found in the file. the given word is the %d word in the file", 
                                                                 word, count);

在上面打印中,如果您打印的是在代码中未赋值的word,而只是打印wd字符串。

票数 0
EN

Stack Overflow用户

发布于 2014-02-06 00:49:39

哈哈哈,原来输出应该是字符串,而不是单词,问题解决了

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21565933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档