阅读外部文本文件后,如何将其转换为数组? (查找迷宫的代码)
我想通过外部加载的文件在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您想从文件加载数组
迷宫
的元素Maze.txt
而不是如发布的代码中所示,您会吗请尝试以下片段:
Assuming you want to load the elements of the array
maze
from a filemaze.txt
instead of assigning as shown in the posted code, would youplease try the following fragment: