2

I was getting Segmentation Fault (core dump) error when I run the code. After using some printf statement I found out that there is an error in strcmp part, maybe it's because comparing a char with a string? How do I fix this?

// this function checks if the file contains the *string

bool checkIfMatch(char *string, FILE *file) {

    while (true) {

        char buff[1024];
        fgets(buff, sizeof buff, file);
        if (buff == NULL)
            break;

        char *substring=strstr(buff, string);
        if ((strcmp(buff, substring)) == 0)
            return true;
    }

    return false;    
}
bbonev
  • 1,406
  • 2
  • 16
  • 33
Gavin Z.
  • 423
  • 2
  • 6
  • 16
  • use strncmp() instead of strcmp() i.e strlen(substring). – rakib_ Feb 09 '14 at 04:02
  • 3
    This: `if(buff ==NULL) break;` will *never* cause a break; Use `if (fgets(buff, sizeof buff, file) == NULL) break;` You also never check the results of your `strstr` before using them in `strcmp`. – WhozCraig Feb 09 '14 at 04:08
  • and `strcmp` and `strstr` is enough only one or the other of duplicates. – BLUEPIXY Feb 09 '14 at 04:57

4 Answers4

1

You have no guarantee that substring is non-NULL. So you need to test it:

bool checkIfMatch(char *string, FILE *file) {
    char buff[1024];
    while (fgets(buff, sizeof buff, file)) {
        char *substring=strstr(buff, string);
        if (substring && !strcmp(buff, substring)) 
            return true;
    }
    return false;
}

Other problems:

  • In your code if (buff == NULL) break; will never cause a break. You need to test the return value of fgets. (See WhozCraig's comment)

  • fgets keeps the carriage-return, which is probably not what you want.

  • The strstr/strcmp is confused: you probably just want strcmp, or maybe just strstr.

  • If the file has a line longer than 1022 characters, then you might miss finding the string.

Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77
  • 1
    +1 (was waiting for the CRLF detection). And I agree with the `strstr`/`strcmp` confusion, I *think* `strncmp` would do what the OP is going for but hard saying. – WhozCraig Feb 09 '14 at 04:22
0

The array buff is not null terminated. You need to do that for the str... family of functions

PS: Learn to indent the code PPS: Use a debugger to find of the value of buff

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

if the buff is not NULL terminated, then problem will come in the strcmp. In your case, initialize the buff variable(to zero) at the declaration part. Otherwise check the filesize and then add NULL character at the buff:

[filesize+1]='\0'.
Martin Drozdik
  • 12,742
  • 22
  • 81
  • 146
mahendiran.b
  • 1,315
  • 7
  • 13
0
char *substring=strstr(buff, string);
    if ((strcmp(buff, substring)) == 0)
        return true;

char *substring in the case where you have a match, substring is pointing to somewhere inside buf if no match substring is pointing to NULL, and when you do strcmp(buff,substring) this will never be zero.

tesseract
  • 891
  • 1
  • 7
  • 16