read matrix from file in c

Solutions on MaxInterview for read matrix from file in c by the best coders in the world

showing results for - "read matrix from file in c"
Emiliano
11 Mar 2018
1#include <stdio.h>
2
3int readmatrix(size_t rows, size_t cols, int (*a)[cols], const char* filename)
4{
5
6    FILE *pf;
7    pf = fopen (filename, "r");
8    if (pf == NULL)
9        return 0;
10
11    for(size_t i = 0; i < rows; ++i)
12    {
13        for(size_t j = 0; j < cols; ++j)
14            fscanf(pf, "%d", a[i] + j);
15    }
16
17
18    fclose (pf); 
19    return 1; 
20}