Home > Enterprise >  After reading an external text file, how can I convert it to an array? (Code to find the maze)
After reading an external text file, how can I convert it to an array? (Code to find the maze)

Time:04-20

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.

CodePudding user response:

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;
}
  • Related