I'm trying to save a 2-D array of integers to a binary file. The problem is that only the first row of the 2-D array is saved. Below I have attached the outputs from which you will better understand the problem. Thanks in advance for the help.
#include <stdio.h>
#include <stdlib.h>
#define N_MAX 100
// Declaration of functions
int readMatrix(FILE *fp, int matrix[N_MAX][N_MAX]);
void printMatrix(int matrix[N_MAX][N_MAX], int size);
void switchMax(int matrix[N_MAX][N_MAX], int size);
int avg(int matrix[N_MAX][N_MAX], int size);
int sumDown(int matrix[N_MAX][N_MAX], int size);
int sumUp(int matrix[N_MAX][N_MAX], int size);
void sortMainDiagonal(int matrix[N_MAX][N_MAX], int size);
int main()
{
int size, matrix[N_MAX][N_MAX], test[N_MAX][N_MAX];
FILE *fp;
fp = fopen("input.txt", "r");
if (fp != NULL)
{
size = readMatrix(fp, matrix);
fclose(fp);
switchMax(matrix, size);
avg(matrix, size);
sortMainDiagonal(matrix, size);
printf("\nOriginal matrix: \n");
printMatrix(matrix, size);
// SAVE ON BINARY FILE
fp = fopen("output.bin", "w");
if (fp != NULL)
{
fwrite(matrix, sizeof(int), size*size, fp);
fclose(fp);
} else {
printf("Error #e2");
}
// READ FROM BINARY FILE
fp = fopen("output.bin", "r");
if (fp != NULL)
{
fread(test, sizeof(int), size*size, fp);
fclose(fp);
printf("\nMatrix from file: \n");
printMatrix(test, size);
} else {
printf("Error #e3");
}
} else {
printf("Errore #e1");
}
}
// Definition of functions
int readMatrix(FILE *fp, int matrix[N_MAX][N_MAX])
{
int size = 0;
fscanf(fp, "%d", &size);
for (int i = 0; i < size; i )
for (int j = 0; j < size; j )
fscanf(fp, "%d", &matrix[i][j]);
return size;
}
void printMatrix(int matrix[N_MAX][N_MAX], int size)
{
for (int i = 0; i < size; i )
{
for (int j = 0; j < size; j )
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
void switchMax(int matrix[N_MAX][N_MAX], int size)
{
int max = 0, row = 0;
for (int i = 0; i < size; i )
{
for (int j = 0; j < size; j )
{
if (matrix[j][i] > max)
{
max = matrix[j][i];
row = j;
}
}
matrix[row][i] = matrix[i][i];
matrix[i][i] = max;
max = 0;
}
}
int avg(int matrix[N_MAX][N_MAX], int size)
{
int avg;
avg = (sumDown(matrix, size) sumUp(matrix, size)) / 2;
matrix[size-1][size-1] = avg;
return avg;
}
int sumDown(int matrix[N_MAX][N_MAX], int size)
{
int sum = 0;
for (int i = 1; i < size; i )
for (int j = 0; j < i; j )
sum = matrix[i][j];
return sum;
}
int sumUp(int matrix[N_MAX][N_MAX], int size)
{
int sum = 0;
for (int i = 0; i < size-1; i )
for (int j = i 1; j < size; j )
sum = matrix[i][j];
return sum;
}
void sortMainDiagonal(int matrix[N_MAX][N_MAX], int size)
{
int temp;
for (int i = 0; i < size-1; i )
{
for (int j = 0; j < (size-1-i); j )
{
if (matrix[j][j] > matrix[j 1][j 1])
{
temp = matrix[j][j];
matrix[j][j] = matrix[j 1][j 1];
matrix[j 1][j 1] = temp;
}
}
}
}
Screenshot of outputs from 'printMatrix' function: click
File 'input.txt':
4
5 6 1 8
1 20 3 4
9 0 11 12
13 4 15 1
CodePudding user response:
You don't show the value for N_MAX
, but your image (which should be text in the question!) shows a 4x4 matrix. In future, please create an MCVE (Minimal, Complete, Verifiable Example).
However, let's suppose N_MAX
is 16 or more (but that dim
is 4). Then your code to write the matrix is writing a lot of zeroes. And skipping the other data.
Your matrix looks like:
13 6 1 8 0 0 0 0 0 0 0 0 0 0 0 0
1 15 3 4 0 0 0 0 0 0 0 0 0 0 0 0
9 0 20 1 0 0 0 0 0 0 0 0 0 0 0 0
5 4 11 26 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
And the fwrite()
writes the first 16 integer values reading across. As you demonstrated, 12 of those are zeros.
To write a sub-array, you'll have to write the dim
entries of each of the matrix's first dim
rows separately.
for (size_t i = 0; i < dim; i )
{
if (fwrite(matrix[i], sizeof(int), dim, fp) != dim)
err_syserr("short write on row %zu of matrix\n", i);
}
And similarly with the fread()
code.