简体   繁体   中英

Fscanf not copying to string

You know that moment when your trying to learn but you're missing some small detail and just can't figure it out? I'm having one of those and would be extremely grateful if it were over. Anyway here's my code:

// I'm trying to implement a load function for a trie, a dictionary in particular

// I'm getting a segfault when it arrives at strcpy, but when I check the value of 'word' there, it's NULL

bool load(const char *dictionary) {
    FILE *file = fopen(dictionary, "r");
    char *word = NULL;
    char input[45] = { '\0' };
    int size = sizeof(node);

    while (fscanf(file, "%s", word) != EOF) {
        printf("%s", word);
        strcpy(input, word);
        int cycle = 0;
        node *next = &root;

        while (input[cycle] != '\0') {
            int position = toupper(input[cycle]) % 65;
            if (position == 39) {
                position = 26;
            }

            if (next->children[position] == NULL) {
                next->children[position] = malloc(size);
                next = next->children[position];
            } else {
                next = next->children[position];
            }
        }

        *sizePointer = *sizePointer + 1;
        next->wordHere = true;
    }
    fclose(file);
    return true;
}

When using fscanf , you are required to ensure that sufficient memory is allocated for the arguments that it will write to.

For instance you could declare "word" as follows:

char word[100];

This allows for a character string up to 100 characters (including the terminating null character). The code you submitted didn't allocate any space and you are running into undefined behavior. Most likely fscanf is overwriting your "input" variable, but it really depends on the compiler and what compile settings are enabled.

I suggest you forget about the variable "word" altogether and just pass "input" into your fscanf. Then you can remove the call to strcpy as well.

Additionally, fscanf is a function that requires great care when using. Please see this question on SO: When/why is it a bad idea to use the fscanf() function?

You must define word either as an array or as a pointer that points to an actual array. Passing NULL as the destination for the %s conversion specifier has undefined behavior.

Furthermore, you should pass the maximum number of bytes to store into the array to prevent potential buffer overflows.

Finally, the test on the return value should be == 1 instead of != EOF . In this particular case, the behavior would be very much the same, but not in general.

Here is a modified version:

bool load(const char *dictionary) {
    FILE *file = fopen(dictionary, "r");
    char word[45];
    char input[45] = { '\0' };
    int size = sizeof(node);

    if (file == NULL)
        return false;

    while (fscanf(file, "%44s", word) == 1) {
        printf("%s\n", word);
        strcpy(input, word);
        ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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