Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a .txt file that is open and read with the findMatches function. The file have the following structure:

Auckland link42 link24 link23 link5
Wellington link3 link21
Queenstown link1 link9 link15

The request array contain keywords that were input by the user. I was trying to print only tokens that correspond to words in request.

For example:

>>> Auckland Wellington

Auckland
link42
link24
link23
link5

Queenstown
link1
link9
link15

However, I get Segmentation Fault 11. The error is not present if 1 keyword is searched, but only if 3 or more, and it displays only maximum two keywords with corresponding links (as above).

Why is this happening and how can I fix it?

#define MAXSTR 1000

int findMatches (const char *filename, char *request[], int size) {

    char lines[MAXSTR];
    char delim[2] = " ";
    char *token;

    FILE *file; 

    if ((file = fopen(filename, "r")) == NULL) {
        printf("Error.
");
    }

    while(fgets(lines, MAXSTR, file) != NULL) {
        token = strtok(lines, delim);
    
        for (int i = 0; i < size; i++) {
            if (!strcmp(request[i], token)) {
                
                    while (token != NULL) {
                        printf("%s
", token);
                        token = strtok(NULL, delim); 
                }
            }
        }
    }
    return 0;
}


int main (int agrc, char *argv[]) {

    char *request[MAXSTR];
    int size = agrc - 1;
    int index = 0;

    for (int i = 1; i < agrc; i++) {
        request[index] = argv[i];
        index++;
    }

    findMatches("index.txt", request, size);

    return 0;
}
question from:https://stackoverflow.com/questions/65649852/segmentation-fault-11-finding-words-in-file-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
194 views
Welcome To Ask or Share your Answers For Others

1 Answer

In this loop

        token = strtok(lines, delim);
    
        for (int i = 0; i < size; i++) {
            if (!strcmp(request[i], token)) {
                
                    while (token != NULL) {
                        printf("%s
", token);
                        token = strtok(NULL, delim); 
                }
            }
        }

When firstly the request is matched to the token, the while loop is executed and token becomes NULL after that. Then, the next iteration of for loop, strcmp(request[i], NULL) is executed and this leads to Segmentation Fault.

You should add break; after the while loop to avoid this.

        token = strtok(lines, delim);
    
        for (int i = 0; i < size; i++) {
            if (!strcmp(request[i], token)) {
                
                    while (token != NULL) {
                        printf("%s
", token);
                        token = strtok(NULL, delim); 
                }
                break; /* you should add this */
            }
        }

Also you shoudn't do fgets(lines, MAXSTR, file) when file is NULL.

    if ((file = fopen(filename, "r")) == NULL) {
        printf("Error.
");
        return -1; /* you should add this */
    }

And you forgot to close the opened file.

    }
    fclose(file); /* you should add this */
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...