C-文件指针在循环的第二次迭代中变为无效



我正在编码一个文本游戏,其中将用户输入与我拥有的文本文件中可用的命令列表进行了比较。

它可以正常工作,但是我希望它循环,以便用户可以在输入命令之前执行多个操作,以打破循环并将程序移动。不幸的是,我的文件指针在循环的第二次迭代中变为NULL,因此完全脱离了程序。

凹痕有些混乱,我对此表示歉意:

    int mansionOutside(void)
    {
        int stop = 1; //control variable for while, yeah I know its a shit method of doing it, sue me
        char choice[25]; //Variable to store user input
        char word_match[15] = "outside"; //counter and .txt will be strcat to it
        char text_line[73]; //size of line for txt files
        char line1[25]; //takes current line being read from file
        char temp[2]; //character and '' so counter can be made into a string and strcat into word_match
        int counter = 1; //counts number of lines
        int stop2 = 1;
        while (stop2 == 1)
        {
            FILE *fptr;
            fptr = fopen("mansion_commands.txt", "r"); //opens file
            int lookdoor_counter = 0;
            printf("%pn", fptr);
            if (fptr == NULL)
            {
                perror("ERROR!"); //Error handling for not opening file
                exit(EXIT_FAILURE);
            }
            else
            {
                while (stop == 1)
                {   
                    puts(""); //newline, as the previous function ends in a text line
                    fgets(choice, sizeof choice, stdin); //store input in choice with buffer overflow protection
                    while (fgets(line1, 25, fptr) != NULL) //reads until newline
                    {
                        if (strcmp(line1, choice) == 0) //if choice is equal to the current line
                        {
                            //printf("%sn", line1); //debugging print
                            if (strcmp(line1, "LOOK door") == 0)
                            {
                                lookdoor_counter++; //so that on entering LOOK door a second time, a different
                        //message will appear
                            }
                        stop = 0; // will exit from loop
                        break; //break ensures we can use the current counter value for later
                    }
                    else
                    {
                        counter++;
                        //printf("%s + %sn", line1, choice); //debugging
                    }
                }
            if  (stop == 1)
            {
                printf("I dont understandn"); 
                //printf("%dn", counter);
                //printf("%sn", line1);
                //printf("%sn", choice);
                counter = 1; //counter is reset to be able to try again
                rewind(fptr);
            }
        }
        fclose(fptr);
        if (lookdoor_counter == 2)
        {
            counter += 1;
        }
        counter = counter + '0'; //convert counter digit to its equivalent ascii
        temp[0] = counter; 
        temp[1] = ''; //temp becomes a string with counter and EOF char
        //printf("%cn", counter);
        strncat(word_match, temp , 1); //wordmatch gets counter strncat to it
        strcat(word_match, ".txt"); /*If the string is found on the second line
        word_match will be (outside2.txt), which contains the appropriate 
        message for that user command*/
        //printf("%sn", word_match); //debugging
        if (strcmp(word_match, "outside4.txt") == 0)
        {
            stop2 = 0;
        }
        fptr = fopen(word_match, "r"); //we open word_match file
        if (fptr == NULL)
        {
            printf("ERROR!n"); //error handling
            exit(EXIT_FAILURE);
        }
        else
        {
            //printf("Debugging : File opened Successfullyn");
            while (fgets (text_line, 72, fptr) != NULL)
            {
                printf("%s", text_line); // Prints every Line with a 2 sec delay
                //delay(2);
            }
        }
    }
    fclose(fptr);
}
return 1;

}

fptr = fopen("mansion_commands.txt", "r"); //opens file
if (!fptr)perror("fopen");

添加此条件,并查看实际错误是什么。也许您在FS上没有许可,或者路径不是真的,等等...

最新更新