fopen неправильно открывает файлы в C - PullRequest
0 голосов
/ 06 августа 2020

Я сейчас работаю над проектом. По какой-то причине fopen () не читает строки из файла, а вместо отправки программы в дамп памяти. В файле несколько строк, и я должен обрабатывать каждую из них отдельно.

 // checks for command line argument indicating an input file is present
    if(argc > 1){
        FILE* fp;
        fp = fopen(argv[1], "r");
        if(fp != NULL){
            //Reading file by line
            while(fgets(userInput, 1024, fp)){
                parentName = strtok(userInput, ",");
        //trim function removes white spaces and move the remaing chars to the left. 
                memmove(parentName, trim(parentName), strlen(trim(parentName)) + 1);
                if(parentName != NULL){
                    // adding element to tree
                    tree = add_child(tree, parentName, childName);
                    childName = strtok(NULL, ",");
                    while(childName != NULL){
                        // trims the child name to remove white-spaces
                        memmove(childName, trim(childName), strlen(trim(childName)) + 1);
                        tree = add_child(tree, parentName, childName);
                        childName = strtok(NULL, ",");
                    }
                }
            }
            // closes the file
            fclose(fp);
        }
        // the file could not be opened for reading
        else
            fprintf(stderr, "error: could not open file '%s'\n", argv[1]);
    }
...