阅读外部文本文件后,如何将其转换为数组? (查找迷宫的代码)

发布于 2025-01-23 00:54:00 字数 1869 浏览 0 评论 0原文

我想通过外部加载的文件在int迷宫[row] [col]函数中创建一个数组。

text.txt中的内容在int main

11111111111111111111111111111111111111111 10011111111111111111111
11000011111110000001111
11011011111111101101111
1100110000110110110111
11101111111110110110111
1110111111100111111111
11100000111111111111111
11111111111111110000111
11111111110000000111111
1000000111111111111111111
11111111110000000000001
111111111111111111111

我正在发布一部分代码以帮助解释。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define ROW 13
#define COL 24
#define STACK_SIZE ROW * COL
#define TRUE 1
#define FALSE 0
int maze[ROW][COL] = {
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1},
    {1,1,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1},
    {1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,0,1,1,1},
    {1,1,0,0,1,1,0,0,0,0,0,0,1,1,0,1,1,0,1,1,0,1,1,1},
    {1,1,1,0,1,1,1,1,1,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1},
    {1,1,1,0,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,0,1,1,1},
    {1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1},
    {1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1},
    {1,1,1,1,1,1,1,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1},
    {1,0,0,0,0,0,0,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1},
    {1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
}; 

省略的代码包括寻路代码和输出代码。

int main(void)
{
    printf("<<<INPUT MAZE>>>\n");
    FILE* fp = fopen("maze.txt", "r");
    char c;
    while ((c = fgetc(fp)) != EOF) {
        printf("%c", c);
    }
    printf("\n");


    for (i = 0; i < ROW; i++)
    {
        for (j = 0; j <= COL; j++)
        {
            mark[i][j] = 0;
        }
    }

    path();
    print_path();

    return 0;
}

我试图上传代码,但由于错误而失败。对不起。

I want to create an array inside the int maze[ROW][COL] function through an externally loaded file.

content in text.txt in int main

111111111111111111111111
100111111111111110111111
110000111111110000001111
110110111111110110110111
110011000000110110110111
111011111110110110110111
111011111110000111110111
111000001111111111110111
111111101111111110000111
111111101110000000111111
100000011110111111111111
111111111110000000000001
111111111111111111111111

I'm posting a part of my code to help explain.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define ROW 13
#define COL 24
#define STACK_SIZE ROW * COL
#define TRUE 1
#define FALSE 0
int maze[ROW][COL] = {
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
    {1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1},
    {1,1,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1},
    {1,1,0,1,1,0,1,1,1,1,1,1,1,1,0,1,1,0,1,1,0,1,1,1},
    {1,1,0,0,1,1,0,0,0,0,0,0,1,1,0,1,1,0,1,1,0,1,1,1},
    {1,1,1,0,1,1,1,1,1,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1},
    {1,1,1,0,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,0,1,1,1},
    {1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1},
    {1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1},
    {1,1,1,1,1,1,1,0,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1},
    {1,0,0,0,0,0,0,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1},
    {1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
}; 

The omitted code includes a wayfinding code and an output code.

int main(void)
{
    printf("<<<INPUT MAZE>>>\n");
    FILE* fp = fopen("maze.txt", "r");
    char c;
    while ((c = fgetc(fp)) != EOF) {
        printf("%c", c);
    }
    printf("\n");


    for (i = 0; i < ROW; i++)
    {
        for (j = 0; j <= COL; j++)
        {
            mark[i][j] = 0;
        }
    }

    path();
    print_path();

    return 0;
}

I tried to upload the code, but it failed with an error. Sorry.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丶情人眼里出诗心の 2025-01-30 00:54:00

假设您想从文件加载数组迷宫的元素
Maze.txt而不是如发布的代码中所示,您会吗
请尝试以下片段:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _CRT_SECURE_NO_WARNINGS
#define ROW 13
#define COL 24
#define TRUE 1
#define FALSE 0
#define BUF 256                 // line buffer size
#define MAZE "maze.txt"         // filename to read

int main(void)
{
    FILE *fp;
    char buf[BUF];
    int maze[ROW][COL];
    int i, j;

    printf("<<<INPUT MAZE>>>\n");
    if (NULL == (fp = fopen(MAZE, "r"))) {
        fprintf(stderr, "Can't open %s\n", MAZE);
        exit(1);
    }

    for (i = 0; i < ROW; i++) {
        if (! fgets(buf, BUF, fp)) {
            fprintf(stderr, "Too few lines in %s\n", MAZE);
            exit(1);
        }
        if (strlen(buf) <= COL) {
            fprintf(stderr, "Too few columns in %s\n", buf);
            exit(1);
        }
        for (j = 0; j < COL; j++) {
            maze[i][j] = buf[j] == '0' ? FALSE : TRUE;
        }
    }

    // check if the array is properly assigned
    for (i = 0; i < ROW; i++) {
        for (j = 0; j < COL; j++) {
            printf("%d", maze[i][j]);
        }
        printf("\n");
    }

    // put your remaining code here

    return 0;
}

Assuming you want to load the elements of the array maze from a file
maze.txt instead of assigning as shown in the posted code, would you
please try the following fragment:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _CRT_SECURE_NO_WARNINGS
#define ROW 13
#define COL 24
#define TRUE 1
#define FALSE 0
#define BUF 256                 // line buffer size
#define MAZE "maze.txt"         // filename to read

int main(void)
{
    FILE *fp;
    char buf[BUF];
    int maze[ROW][COL];
    int i, j;

    printf("<<<INPUT MAZE>>>\n");
    if (NULL == (fp = fopen(MAZE, "r"))) {
        fprintf(stderr, "Can't open %s\n", MAZE);
        exit(1);
    }

    for (i = 0; i < ROW; i++) {
        if (! fgets(buf, BUF, fp)) {
            fprintf(stderr, "Too few lines in %s\n", MAZE);
            exit(1);
        }
        if (strlen(buf) <= COL) {
            fprintf(stderr, "Too few columns in %s\n", buf);
            exit(1);
        }
        for (j = 0; j < COL; j++) {
            maze[i][j] = buf[j] == '0' ? FALSE : TRUE;
        }
    }

    // check if the array is properly assigned
    for (i = 0; i < ROW; i++) {
        for (j = 0; j < COL; j++) {
            printf("%d", maze[i][j]);
        }
        printf("\n");
    }

    // put your remaining code here

    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文