user3741086
user3741086

Reputation: 125

Read file into multidimensional character array in C

I am trying to read a Multidimensional String Matrix from a file and store it to a array of Character Arrays like this:

char *A[M][N];

This works fine if I stay inside the main function. But when i am trying to call a function by reference it doesn't work.

Function Call:

readMatrix(file,A);

Function Header:

int readMatrix(FILE *file,char *matrix[][]);

I also tried this:

int readMatrix(FILE *file,char ***matrix);

which also doesn't work. I want to manipulate the Array in the function therefore i need to make a reference call.

Upvotes: 2

Views: 658

Answers (2)

chqrlie
chqrlie

Reputation: 144595

You must pass N as part of the matrix type to your ReadMatrix function, and since it is not known at compile time, you must pass these as arguments too:

int readMatrix(FILE *file, size_t M, size_t N, char *matrix[][N]);

You could indeed also specify the array argument as char *matrix[M][N], but the first dimension size M is ignored for a function argument as it only receives a pointer to the array.

Here is an example:

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

int readMatrix(FILE *file, size_t rows, size_t cols, char *matrix[][cols]) {
    int rc = 0;
    for (size_t i = 0; i < rows; i++) {
        for (size_t j = 0; j < cols; j++) {
            char buf[80];
            if (rc == 0 && fscanf(file, "%79s", buf) == 1) {
                matrix[i][j] = strdup(buf);
            } else {
                matrix[i][j] = NULL;
                rc = -1;
            }
        }
    }
    return rc;
}

int main(void) {
    size_t rows, cols;
    if (scanf("%zu %zu", &rows, &cols) != 2)
        return 1;
    char *mat[rows][cols];
    if (readMatrix(stdin, rows, cols, mat) == 0) {
        for (size_t i = 0; i < rows; i++) {
            for (size_t j = 0; j < cols; j++) {
                printf(" %8s", mat[i][j]);
            }
            printf("\n");
        }
    }
    for (size_t i = 0; i < rows; i++) {
        for (size_t j = 0; j < cols; j++) {
            free(mat[i][j]);
        }
    }
    return 0;
}

Upvotes: 1

ffguven
ffguven

Reputation: 185

You can do it by using this approach:

#include <stdio.h>
#define MAX_WORD_SIZE 10
void readMatrix(FILE*,char****);
int main(void){
    FILE* cin = fopen("input.txt","r");
    char*** inputMatrix;
    readMatrix(cin,&inputMatrix);
    for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                printf("%s ",*(*(inputMatrix+i)+j));
            }
            printf("\n");
    }
    fclose(cin);
    return 0;
}
void readMatrix(FILE* cin,char**** matrix){
    int rowNum,columnNum;
    char buff[10];
    int intBuff;
    fscanf(cin,"%d",&intBuff);
    rowNum = intBuff;
    fscanf(cin,"%d",&intBuff);
    columnNum = intBuff;
    *matrix = malloc(rowNum*sizeof(char**));
    for(int i=0;i<rowNum;i++){
        *(*matrix+i) = malloc(columnNum*sizeof(char*));
    }

    for(int i=0;i<rowNum;i++){
        for(int j=0;j<columnNum;j++){
            *(*(*matrix+i)+j) = malloc(MAX_WORD_SIZE*sizeof(char));
            fscanf(cin,"%s",*(*(*matrix+i)+j));
        }
    }
}

with this input file(input.txt file within same folder), this code is working well on Linux GCC

3 3
ab cd ef
gh ij kl
mn op qr

In the first line of input.txt file, first integer denotes row number, second integer denotes column number.

Upvotes: 0

Related Questions