在我的程序中,我使用户能够决定其矩阵的维度,然后在矩阵中输入每个单元格的值。然后将该矩阵作为二进制数据保存到 .txt 文件中。有时,对于具有某些值和维度的某些矩阵,无法在正确单元格中正确读取正确值的矩阵。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
int numberOfMatrices = 0;
int orderedMatricesInfo[100][1][2];
int matrixRows = 0;
int matrixCols = 0;
int** initMatrix(int rows, int cols);
void saveMatrix(int** matrix, int rows, int cols);
void* allocateMatrix(int rows, int cols);
int** readMatrix(int rows, int cols, int matrix[matrixRows][matrixCols], int matrixNum);
void displayMatrices();
void displayMatrix(int rows, int cols, int** matrix);
int main()
{
int n, m;
int** matrixPointer;
printf("Please enter the number of rows in your matrix: \n");
scanf("%d", &n);
printf("Please enter the number of columns in your matrix: \n");
scanf("%d", &m);
matrixPointer = initMatrix(n, m);
printf("%d\n", matrixPointer[0][0]);
displayMatrices();
printf("SUCCESS");
}
int** initMatrix(int rows, int cols)
{
int matrix[rows][cols];
int **matrixPointer;
matrixPointer = malloc(sizeof(int*)*cols);
for (int i =0; i < cols; ++i)
{
matrixPointer[i] = malloc(sizeof(int*)*cols);
}
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
printf("\nPlease enter element for [%d][%d]: \n", i, j);
scanf("%d", &matrixPointer[i][j]);
}
}
++numberOfMatrices;
orderedMatricesInfo[numberOfMatrices-1][0][0] = rows;
orderedMatricesInfo[numberOfMatrices-1][0][1] = cols;
allocateMatrix(rows, cols);
saveMatrix(matrixPointer, rows, cols);
return matrixPointer;
}
void saveMatrix(int** matrix, int rows, int cols)
{
char fullFileName[100] = "matrix";
char fileNameExtension[100] = ".txt";
char strNum[100];
sprintf(strNum, "%d", numberOfMatrices);
strcat(strNum, fileNameExtension);
strcat(fullFileName, strNum);
FILE *file = fopen(fullFileName, "ab");
for(int i=0; i<rows; ++i)
{
fwrite(matrix[i], sizeof(int), rows, file);
}
fclose(file);
}
void* allocateMatrix(int rows, int cols)
{
return malloc(sizeof(int[rows][cols]));
}
int** readMatrix(int rows, int cols, int matrix[matrixRows][matrixCols], int matrixNum)
{
int **matrixPointer;
matrixPointer = malloc(sizeof(int*)*cols);
for (int i =0; i < cols; ++i)
{
matrixPointer[i] = malloc(sizeof(int*)*cols);
}
char fullFileName[100] = "matrix";
char fileNameExtension[100] = ".txt";
char strNum[100];
sprintf(strNum, "%d", matrixNum);
strcat(strNum, fileNameExtension);
strcat(fullFileName, strNum);
FILE *file;
file=fopen(fullFileName, "rb");
fread(matrix, sizeof(int[rows][cols]), 1, file); // read 1 2D-array
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
matrixPointer[i][j] = matrix[i][j];
printf("%d", matrixPointer[i][j]);
}
}
allocateMatrix(rows, cols);
return matrixPointer;
}
void displayMatrices()
{
for (int matrixNum = 1; matrixNum <= numberOfMatrices; ++matrixNum)
{
char fullFileName[100] = "matrix";
char fileNameExtension[100] = ".txt";
char strNum[100];
sprintf(strNum, "%d", matrixNum);
strcat(strNum, fileNameExtension);
strcat(fullFileName, strNum);
matrixRows = orderedMatricesInfo[matrixNum -1][0][0];
matrixCols = orderedMatricesInfo[matrixNum -1][0][1];
int matrix[matrixRows][matrixCols];
int** matrixPointer;
matrixPointer = readMatrix(matrixRows, matrixCols, matrix, matrixNum);
printf("matrix %d", matrixNum);
displayMatrix(matrixRows, matrixCols, matrixPointer);
}
}
void displayMatrix(int rows, int cols, int** matrix)
{
for (int i = 0; i < rows; ++i)
{
printf("\n");
for (int j = 0; j < cols; ++j)
{
printf("%d ", matrix[i][j]);
}
}
printf("\n");
}
在主函数中,当用户创建矩阵时,矩阵作为二进制数据保存在一个新创建的名为“matrix1.txt”的文件中。但是,我认为它没有正确存储,因为当回读时,矩阵没有正确显示正确的值。例如,如果我创建一个矩阵:
[12, 19, 17, 15,
120, 566, 214, 153,
124, 1, 2, 3]
然后尝试显示矩阵然后上面的矩阵没有显示,而是显示:
[12, 19, 17, 120,
566, 214, 124, 1,
2, 0, 101792956, 1]
我是否错误地为该程序分配了内存?会有什么问题?因为它确实设法检索并正确显示了顶行的前 3 个元素。我是 C 新手,所以我无法查明问题所在。当矩阵像 2x2 矩阵一样小时,它可以正常工作......
谢谢!