为什么文件就打不开了,如何恢复文件可以打开的状态呢(标签-C语言|关键词-打开文件)

C语言,打开文件,然后关闭,之后再关闭,为什么文件就打不开了,如何恢复文件可以打开的状态呢?代码的思路不会了。

可能资源泄漏了,重启过也是这种情况吗?可以用一个代码判断关闭状态:

#include <stdio.h>

int main() {
    FILE *fp;
    fp = fopen("test.txt", "r"); 
    if(fp == NULL) { 
        printf("无法打开\n");
        return 1;
    }
    fclose(fp);

    return 0;
}


在C语言中,打开文件后需要及时关闭文件,否则可能会导致文件句柄被占用,无法再次打开文件。如果文件无法打开,可以尝试以下方法恢复文件状态:

重启计算机,有时候文件句柄会被释放。

确认文件是否被其他程序占用,如果是,关闭占用程序。

检查文件是否已被删除或移动到其他位置。

如果文件仍无法打开,可以尝试使用文件恢复软件进行恢复。

注意:在操作文件时,应该遵循良好的文件处理习惯,及时关闭文件,避免文件损坏或丢失。

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/249853
  • 这篇博客也不错, 你可以看下数据结构-检查C文件符号匹配-C语言-【检查C源文件中{}、()等括号是否匹配,(包含引号注释) 并输出第一个检测到的不匹配的括号及所对应括号所在的行号(程序中只有一个括号不匹配)。】
  • 除此之外, 这篇博客: 用链表来实现贪吃蛇游戏中的 贪吃蛇是我们很经常玩的小游戏,而如果用链表来做一个贪吃蛇游戏,你就会对链表有一个深刻的认识,而且链表作为c语言的重点,也是c语言的独特魅力所在 算了,不说那么多了,上干货 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    **
    snake.c

    #include <curses.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include "LIST.h"
    #define MAP_WIDTH 20
    #define MAP_HEIGHT 20
    extern struct Snake *g_snake;
    extern struct Snake g_food;
    
    int g_flag = 0;
    
    int g_direction = KEY_RIGHT;
    int commitsuicide() {
        struct Snake *p;
        p = g_snake;
        struct Snake *tmp;
        tmp = g_snake->next;
        while (tmp != NULL) {
            if ((p->x == tmp->x) && (p->y == tmp->y)) {
                return 0;
            }
            tmp = tmp->next;
        }
        return -1;
    }
    
    void initFood() {
        int judge = 0;
        do {
            int x = rand() % 20 + 1;
            int y = rand() % 20 + 1;
            g_food.x = x;
            g_food.y = y;
            g_food.next = NULL;
            if ((g_food.x == g_snake->x) && (g_food.y == g_snake->y)) {
                judge = 1;
            }
        } while (g_food.x == 0 || g_food.x > 18 || g_food.y == 0 || g_food.y > 18 ||
                 judge == 1);
    }
    
    int foodFind(int x, int y) {
    
        if ((g_food.x == x) && (g_food.y == y)) {
            return 0;
        }
    
        return -1;
    }
    int headTofood() {
    
        if ((g_food.x == g_snake->x) && (g_food.y == g_snake->y)) {
            return 0;
        }
        return -1;
    }
    void initMap() {
        int x;
        int y;
        for (x = 0; x < MAP_WIDTH; x++) {
            for (y = 0; y < MAP_HEIGHT; y++) {
                if (x == 0 || x == MAP_HEIGHT - 1) {
                    if (x == 0) {
                        printw("* ");
                    } else {
                        printw("* ");
                    }
                } else {
                    if (y == 0 || y == MAP_WIDTH - 1) {
                        printw("* ");
                    } else {
                        if ((snakeFind(g_snake, x, y) == 0)) {
                            printw("[]");
                        } else if (foodFind(x, y) == 0) {
                            printw("++");
                        } else {
                            printw("  ");
                        }
                    }
                }
            }
            printw("\n");
        }
    }
    
    void handleKey() {
        int key;
        noecho();
        keypad(stdscr, TRUE);
        while (1) {
            key = getch();
            if (adjustDirection(key) == 0) {
                continue;
            }
        }
    }
    
    int adjustDirection(int key) {
        if (key == KEY_DOWN || key == KEY_UP || key == KEY_RIGHT ||
            key == KEY_LEFT) {
            if (((g_direction == KEY_RIGHT) && (key == KEY_LEFT)) ||
                ((g_direction == KEY_LEFT) && (key == KEY_RIGHT)) ||
                ((g_direction == KEY_UP) && (key == KEY_DOWN)) ||
                ((g_direction == KEY_DOWN) && (key == KEY_UP))) {
                return 0;
            }
        }
        g_direction = key;
        return -1;
    }
    
    void moveSnakeauto() {
        while (1) {
            switch (g_direction) {
                case KEY_DOWN:
                    if (headTofood() == 0) {
                        insertSnake(&g_snake, g_snake->x + 1, g_snake->y);
                        g_flag = -1;
                    } else {
                        insertSnake(&g_snake, g_snake->x + 1, g_snake->y);
                        deleteSnake(&g_snake);
                    }
                    break;
                case KEY_UP:
                    if (headTofood() == 0) {
                        insertSnake(&g_snake, g_snake->x - 1, g_snake->y);
                        g_flag = -1;
                    } else {
                        insertSnake(&g_snake, g_snake->x - 1, g_snake->y);
                        deleteSnake(&g_snake);
                    }
                    break;
                case KEY_LEFT:
                    if (headTofood() == 0) {
                        insertSnake(&g_snake, g_snake->x, g_snake->y - 1);
                        g_flag = -1;
                    } else {
                        insertSnake(&g_snake, g_snake->x, g_snake->y - 1);
                        deleteSnake(&g_snake);
                    }
                    break;
                case KEY_RIGHT:
                    if (headTofood() == 0) {
                        insertSnake(&g_snake, g_snake->x, g_snake->y + 1);
                        g_flag = -1;
                    } else {
                        insertSnake(&g_snake, g_snake->x, g_snake->y + 1);
                        deleteSnake(&g_snake);
                    }
                    break;
                default:
                    break;
            }
            clear();
            if (g_flag == -1) {
                initFood();
                g_flag = 0;
            }
    
            if (snakeDie() == 0 || commitsuicide() == 0) {
    
                destorySnake();
                createSnake();
            }
            initMap();
            refresh();
            usleep(100000);
        }
    }
    
    int main() {
    
        pthread_t t_movesnake;
        pthread_t t_getinput;
    
        initscr();
        createSnake();
        initFood();
        initMap();
        pthread_create(&t_movesnake, NULL, (void *)moveSnakeauto, NULL);
        pthread_create(&t_getinput, NULL, (void *)handleKey, NULL);
        while (1);
        endwin();
        return 0;
    }
    
    

    list.c

    #include <curses.h>
    #include <stdlib.h>
    #include "LIST.h"
    struct Snake *g_snake;
    struct Snake g_food;
    int snakeDie()  // panduansiwang
    {
        if (g_snake->x == 0 || g_snake->x == 19 || g_snake->y == 0 ||
            g_snake->y == 19) {
            return 0;
        }
        return -1;
    }
    void destorySnake()  // bianlishanchu
    {
        struct Snake *p;
        while (g_snake != NULL) {
            p = g_snake;
            g_snake = g_snake->next;
            free(p);
        }
    }
    void insertSnake(struct Snake **head, int x, int y)  // touchafa
    {
        struct Snake *node;
        node = (struct Snake *)malloc(sizeof(struct Snake));
        node->x = x;
        node->y = y;
        node->next = NULL;
        node->next = *head;
        *head = node;
    }
    void createSnake() {//chushihuashe
        g_snake = (struct Snake *)malloc(sizeof(struct Snake));
        g_snake->x = 3;
        g_snake->y = 3;
        g_snake->next = NULL;
        insertSnake(&(g_snake), 3, 4);
        insertSnake(&(g_snake), 3, 5);
        insertSnake(&(g_snake), 3, 6);
    }
    
    int snakeFind(struct Snake *head, int x, int y) {//bianlipanduan
        struct Snake *p;
        p = head;
        while (p != NULL) {
            if ((p->x == x) && (p->y == y)) {
                return 0;
            }
            p = p->next;
        }
        return -1;
    }
    
    void deleteSnake(struct Snake **head) {//shanchuweiba
        struct Snake *p;
        struct Snake *tmp;
        tmp = NULL;
        p = *head;
        while ((p != NULL) && (p->next != NULL)) {
            if ((p->next->next == NULL)) {
                tmp = p->next;
                p->next = NULL;
                free(tmp);
                break;
            }
            p = p->next;
        }
    }
    
    

    list.h

    #ifndef _LIST_H_
    #define _LIST_H_
    
    struct Snake 
    {
        int x;
        int y;
        struct Snake *next;
    };
    void insertSnake(struct Snake **head,int x,int y);
    void createSnake();
    int snakeFind(struct Snake *head,int x,int y);
    void deleteSnake(struct Snake **head);
    int foodFind(int x,int y);
    int snakeDie();
    void  destorySnake();
    
    
    
    
    
    
    
    
    
    #endif
    
    

    Makefile

    snake:
    	gcc snake_final.c LIST.c -lncurses -lpthread   -o snakepro
    
  • 您还可以看一下 贺利坚老师的C语言及程序设计初步课程中的 表达“条件”——条件表达式小节, 巩固相关知识点

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^