#include "stdafx.h" #include int** readTextMatrix(char *path, int *n, int *m) { FILE *f = fopen(path, "r"); fscanf(f, "%d %d", n, m); int **matr = (int**)calloc(*n, sizeof(int*)); for (int i = 0; i < *n; i++) matr[i] = (int*)calloc(*m, sizeof(int)); for (int i = 0; i < *n; i++) for (int j = 0; j < *m; j++) fscanf(f, "%d", &matr[i][j]); fclose(f); return matr; } void freeMatrix(int **matr, int n) { for (int i = 0; i < n; i++) free(matr[i]); free(matr); } void writeBinaryMatrix(char *path, int **matr, int n, int m) { FILE *f = fopen(path, "wb"); fwrite(&n, sizeof(int), 1, f); fwrite(&m, sizeof(int), 1, f); for (int i = 0; i < n; i++) fwrite(matr[i], sizeof(int), m, f); fclose(f); } int** readBinaryMatrix(char *path, int *n, int *m) { FILE *f = fopen(path, "rb"); fread(n, sizeof(int), 1, f); fread(m, sizeof(int), 1, f); int **matr = (int**)calloc(sizeof(int*), *n); for (int i = 0; i < *n; i++) { matr[i] = (int*)calloc(sizeof(int), *m); fread(matr[i], sizeof(int), *m, f); } return matr; } int main() { FILE *f; int n, m; int **matr = readTextMatrix("e:\\1.txt", &n, &m); writeBinaryMatrix("e:\\1.bin", matr, n, m); freeMatrix(matr, n); return 0; }