Home > OS >  Read a file and save the lines in an array with c
Read a file and save the lines in an array with c

Time:09-22

I'm trying to read a given file and write its lines into an array. At first, i'm not dynamically allocating the variable (maze) that will store the lines of the file. This is my code until now.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(void)
{
 
    char c, maze[300][300], buffer[1000] = {'\0'};
    int ch = 0, column, row, columns = 0, rows = 0;
    
    FILE* file = fopen("boards/0.txt", "r ");

    if ( file )
    {
        while( fgets(buffer, sizeof(buffer), file) != NULL)
            rows =1;
    }
    // rows will count the number of rows 
    else
    {
        printf("There's no such file.\n");
        exit(1);
    }
    // and  columns  will count the number of columns
    columns = (strlen(buffer) - 1);

    // write the content file into an matriz
    for (row = 0; row < rows ; row  )
    {
  
        for (column = 0; column < columns; column  )
        {
          
            if(fscanf(file, "%c", &maze[row][column]) != 1)
                exit(1);

        }
        
    }

    fclose(file);
    // print rows
    for (row = 0; row < rows; row  )
    {
        
        for (column = 0; column < columns; column  )
        {
            printf("%c",maze[row][column]);
            

        }
        
    }

    
    return 0;

}

This is the input file:

....*.....................
..........................
........*........*........
.....*....................
...............*....*.....
..*.......*...............
............*.............
..........................
..............*...........
..................*.......
..*.......*......*........
....*..*..................
...**.....................
..........*...............
....................*.....
..........................
....**....................
......................*...

The output should be the same, but nothing happens. I understand that I'm supposed to dynamically allocate the array, but I'm not sure how to fit this into the code to make it work.

CodePudding user response:

The main problem of your solution is that you did not reset the file position after finishing the first read. You should use fseek(file, SEEK_SET, 0) before the second read.

The other problem was that you read the newline characters with fscanf into a maze position, I suppose that you did not want that :)

You can do this process in one pass:

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char maze[300][300] = { 0 };
    int column = 0, row = 0, columns = 0, rows = 0;

    FILE* file = fopen("boards/0.txt", "r");
    if (file) {
        while (fgets(&maze[rows][0], 300, file) != NULL) {
            rows  ;
        }

        columns = strlen(&maze[0][0]) - 1;
    } else {
        printf("There's no such file.\n");
        return 1;
    }

    fclose(file);
    // print rows
    for (row = 0; row < rows; row  ) {
        for (column = 0; column < columns; column  ) {
            printf("%c", maze[row][column]);
        }
        printf("\n");
    }

    return 0;
}
  • Related