BSmanager 2022-10-08 15:12 采纳率: 50%
浏览 76
已结题

vs2022无法打开文件


 
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>  /* printf, scanf, NULL */
#include <stdlib.h>  /* malloc, free, rand, system */
#include <stdbool.h>
//坐标点类 
struct Point {
    int x;
    int y;
    int c;
};
//自定义队列 建议自己输入大小
struct que
{
    int x, y, s, f;//分别存储横、纵、已走过的路程、上一步坐标
}node[250];
//构造坐标点 
struct Point buildPoint(int x, int y,int c)/////////
{
    struct Point p;
    p.x = x;
    p.y = y;
    p.c = c;
    return p;
}
 
//节点 
struct Node {
    struct Point data;
    struct Node* next;
};
 
 
//自定义堆栈 
struct stack {
    struct Node* head;//头节点,之后采用头插法插入节点 
 
    void (*pop)(struct stack* s);//出栈 
 
    void (*push)(struct stack* s, struct Point x);//入栈
 
    struct Point(*top)(struct stack* s);//返回栈顶元素 
 
    int (*empty)(struct stack* s);//栈是否为空 
};
 
void pop(struct stack* s)//出栈 
{
    if (!s->head->next)
        return;
    struct Node* t = s->head->next;//
    s->head->next = s->head->next->next;
    free(t);
}
 
void push(struct stack* s, struct Point x)//入栈
{
    struct Node* t = (struct Node*)malloc(1);
    t->data = x;
    t->next = s->head->next;//
    s->head->next = t;
}
 
struct Point top(struct stack* s)//返回栈顶元素 
{
    return s->head->next->data;
}
 
int empty(struct stack* s)//栈是否为空 
{
    return s->head->next ? 0 : 1;
}
 
//构造堆栈 
struct stack buildStack()
{
    struct Node* head = (struct Node*)malloc(1);
    head->next = NULL;
    struct stack s = { head,pop,push,top,empty };
    return s;
}
 
void testStack()
{
    
    struct Point p1 = buildPoint(1, 1,0);
    struct Point p2 = buildPoint(1, 1, 0);
    struct Point p3 = buildPoint(1, 1, 0);
    struct stack s = buildStack();
    s.push(&s, p1);
    s.push(&s, p2);
    s.push(&s, p3);
    while (!s.empty(&s))
    {
        struct Point p = s.top(&s);
        s.pop(&s);//
        printf("(%d,%d)\n", p.x, p.y);
    }
}
 
int main(void)
{
    struct stack s = buildStack();
    struct Point p1;
    FILE* source;
    char ch[250];
    int head = 1;
    int tail = 1;
    int final_x, final_y;
    int start_x, start_y;
    int tx, ty;//下一步位置
    bool flag = false;//用于结束双层循环
    printf("Enter the name of file:\n");
    scanf("%s", &ch);
    if ((source = fopen("ch", "r")) == NULL)//检测文件打开
    {
        fprintf(stderr, "Can't open file %s", ch);
![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/724640312566170.png "#left")

        exit(1);
    }
    printf("Enter the start of file:\n");
    fseek(source, 0L, SEEK_SET);//定位到文件开始位
    //读取文件至链表
    int n;
    int i = 0;
    int j = 0;
    n = getc(source);
    while (n != '\0')
    {
        if (n == '\n')
        {
            i++;
            j = 0;
        }
        p1 = buildPoint(i, j,n);
        j++;
        s.push(&s, p1); 
        n = getc(source);
    }
    //得到大小
    int size_x, size_y;
    size_y = i + 1;
    size_x = j + 1;
    printf("\tsize_x:%d \tsize_y:%d\n", size_x, size_y);
    //得到出口和入口
    while (!s.empty(&s))
    {
        struct Point p = s.top(&s);
        s.pop(&s);
        if (p.c == 0)
        {
            if (p.x == 0 || p.y == 0||p.x == size_y || p.y == size_x)
            {
                final_x = p.x;
                final_y = p.y;
                break;
            }
 
        }
    }
    printf("Enter the start of file:\n");
    scanf("%d %d", &start_x, &start_y);
    //释放内存
    while (!s.empty(&s))
    {
        struct Point p = s.top(&s);
        s.pop(&s);
    }
    //存储地图(变长数组)建议自行修改代码输入
    int fill[255][255];
    //存储使用状态 建议自行修改代码输入
    int book[255][255];
    //将数据读入二维数组
    while (!s.empty(&s))
    {
        
        struct Point p = s.top(&s);
        s.pop(&s);//
        fill[p.x][p.y] == p.c;
    }
    //初始化使用状态
    for (i = 0; i < size_x; i++)
    {
        for (j = 0; j < size_y; j++)
        {
            book[i][j] == 0;
        }
    }
        //初始化迷宫入口
        book[start_x][start_y] = 1;
        node[tail].y = start_y;
        node[tail].x = start_x;
        node[tail].f = 0;
        node[tail].s = 0;
        tail++;
        while (head < tail)
        {
            //控制下一步行走方向
            for (int ctrl = 0; ctrl < 4; ctrl++)
            {
                switch (ctrl)
                {
                case 0:
                    tx = node[head].x + 1;
                    ty = node[head].y;
                    break;
                case 1:
                    tx = node[head].x + 1;
                    ty = node[head].y;
                    break;
                case 2:
                    tx = node[head].x + 1;
                    ty = node[head].y;
                    break;
                case 3:
                    tx = node[head].x + 1;
                    ty = node[head].y - 1;
                    break;
                }
                //方格可走条件;0、未走过、非终点
                if (tx < 0 || tx > size_x - 1 || ty < 0 || ty > size_y - 1)
                    continue;
                if (fill[ty][tx] == 0 && book[ty][tx] == 0)
                {
                    node[tail].s = node[head].s + 1;
                    node[tail].x = tx;
                    node[tail].y = tx;
                    node[tail].f = tx;
                    book[ty][tx] = 1;
                    tail++;
                }
                if (tx == final_x && ty == final_y)
                {
                    flag = true;
                    break;
                }
                if (flag)
                    break;
                head++;
            }
        }
        
    //打印路程和位置坐标
        printf("%d\n", node[tail - 1].s);
        printf("(%d , %d)\n", final_x,final_y);
        for (i = 0; i < node[tail - 1].s; i++)
        {
            printf("%d", node[tail - 1].s);
 
        }
        int temp = node[tail - 1].f;
            while (temp != 0)
            {
                printf("(%d , %d)\n", node[temp].x, node[temp].y);
                fill[node[temp].x][node[temp].y] = 2;
                temp = node[temp].f;
            }
            //打印小地图
        fill[final_x][final_y] = 2;
        int x, y;
        for ( x = 0; x < size_x; x++)
        {
            for (y = 0; y < size_y; y++)
                printf("%d\n", fill[x][y]);
        }
        //关闭文件
    fseek(source, 0L, SEEK_SET);
    if (fclose(source) != 0)
    {
        fprintf(stderr, "Can't close file %s", source);
        exit(1);
    }
    return 0;
}

img


尝试过移动文件的位置,但还是这样

  • 写回答

2条回答 默认 最新

  • 你好-C嘉嘉 2022-10-08 15:21
    关注

    source = fopen("ch", "r") 其中的ch不要加引号
    source = fopen(ch, "r")

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月16日
  • 已采纳回答 10月8日
  • 创建了问题 10月8日

悬赏问题

  • ¥15 关于#linux#的问题(输入输出错误):出现这个界面接着我重新装系统,又让修电脑的师傅帮我扫描硬盘(没有问题)用着用着又卡死(相关搜索:固态硬盘)
  • ¥15 cv::resize不同线程时间不同
  • ¥15 web课程,怎么做啊😭没好好听课 根本不知道怎么下手
  • ¥15 做一个关于单片机的比较难的代码,然后搞一个PPT进行解释
  • ¥15 python提取.csv文件中的链接会经常出现爬取失败
  • ¥15 数据结构中的数组地址问题
  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 会会信号与系统和python的来
  • ¥15 关于#python#的问题