简体   繁体   English

在C中使用fgets时出错

[英]Error while using fgets in C

I'm writing a small program to check how the function strcasestr works. 我正在编写一个小程序来检查功能strcasestr的工作方式。

What the code below does: 下面的代码做什么:

  1. Asks the user to enter a line of text. 要求用户输入一行文本。
    Example first input: blah bla blah blah the Word we are looking for. 第一个输入示例: blah bla blah blah the Word we are looking for. Example second input: Word 第二个输入示例: Word

  2. What the program should print is: Word we are looking for. 该程序应打印的内容是: Word we are looking for.

  3. But it gives me a Segmentation fault (core dumped) error. 但这给了我一个分段错误(核心转储)错误。

I suspect I'm using fgets() incorrectly. 我怀疑我使用的fgets()错误。 When I run the program using scanf to read the input (of course entering the first input withoutspaces) it works and gives me the expected output. 当我使用scanf运行程序以读取输入(当然,输入第一个没有空格的输入)时,它将起作用并提供预期的输出。

Any idea what's causing the segmentation fault error? 知道导致细分错误的原因是什么吗? How to correct this? 如何纠正呢?

#define _GNU_SOURCE
#define max_buff 1000
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
char *answer;
//char * strcasestr (const char *haystack, const char *needle);

void strip_crlf(char* s);

void strip_crlf(char* s)
{
    char* p = strpbrk(s, "\r\n");
    if (p) *p = '\0';
}

int main(){
  char fname[max_buff+1];
  char lname[max_buff+1];

  printf("Enter the line of text you are searching for ");
  //scanf("%s", fname);

  fgets(fname,max_buff,stdin);
  //printf("%s",fname);
  strip_crlf(fname);

  printf("Enter the search term ");
  //scanf("%s", lname);
  fgets(lname,max_buff,stdin);
  strip_crlf(lname);
  //printf("%s",lname);
  if((answer=strcasestr(fname,lname))!=NULL){

  // printf("now we have something in answer ");
    printf("%s\n",answer);
  }
  else
      printf(" strcasestr failed\n");

}

EDITED: to reflect suggestions made in comments/answers below. 编辑:反映以下评论/答案中提出的建议。 the program now prints: 该程序现在打印:

 strcasestr failed

...Ughh. ... Ughh。

Edit2: program works now. Edit2:程序现在可以工作。 thanks for all your help everyone! 谢谢大家的帮助!

You're not checking for a failure from strcasestr() , which IS failing, because you're not stripping \\n or \\r\\n from your inputs. 您不是要从strcasestr()检查失败,因为失败是因为您没有从输入中剥离\\n\\r\\n

The only search that will succeed is if it matches the end of the first input. 唯一会成功的搜索是,如果它匹配第一个输入的结尾。

To strip CRLF: 剥离CRLF:

void strip_crlf(char* s)
{
    char* p = strpbrk(s, "\r\n");
    if (p) *p = '\0';
}

Then just strip_crlf(fname); 然后就是strip_crlf(fname); after fgets . fgets Same for lname . lname相同。

You need to check if the comparison succeeded before trying to print the result: 在尝试打印结果之前,您需要检查比较是否成功:

if (answer) {
    printf("%s\n", answer);
} else {
    printf("No match\n");
}

The reason the comparison is failing is probably because fgets() includes the newline in the buffer, but scanf() doesn't. 比较失败的原因可能是fgets()在缓冲区中包含换行符,而scanf()没有。 You'll need to remove the newline at the end of the string if you don't want it to mess up the comparison. 如果您不希望它使比较混乱,则需要删除字符串末尾的换行符。

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

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