感謝您收到我之前獲得的所有幫助。C將隨機矩陣寫入文件,然後使用格式化的I/O從文件讀取和複製所述矩陣
在下面的程序中,我想創建一個隨機矩陣,將其打印到屏幕上,將其寫入文件,然後從文件中掃描該矩陣的副本並將副本打印到屏幕上,除了當我嘗試從文件中讀取,在我的代碼的算法是不正確,我認爲,
的scanf函數失敗,我不知道爲什麼....
double *matrix_read(FILE *fptr, double *mat, int m, int n) ;
double *matrix_read(FILE *fptr,double *mat, int m, int n) {
int i,j;
double *ptr,x ;
ptr=mat;
if((fptr=fopen("matrixA.txt","r"))==NULL)
{
puts("Cannot Open File");
}
rewind(fptr);
fscanf(fptr,"\n\nrows %d, columns %d\n\n", &m, &n) ;
mat = (double *) malloc(sizeof(double) * m * n) ;
for (i=0; i < m; i++)
{
for (j=0; j < n; j++)
{
fscanf(fptr, " %5.2lf", &x);
*ptr++=x;
}
}
fclose(fptr);
return mat ;
}
在我main
,該功能如下所示:
rand_matrix(MATRIX.matrix, MATRIX.rows, MATRIX.cols); /* populates matrix with random data */
print_matrix(MATRIX.matrix, MATRIX.rows, MATRIX.cols) ; /* prints the matrix */
matrix_write(fptr, MATRIX.matrix, MATRIX.rows, MATRIX.cols) ; /* writes matrix to file*/
_flushall();
getchar();
MATRIX1.matrix=matrix_read(fptr, MATRIX.matrix, MATRIX.rows, MATRIX.cols) ; /* reads above matrix from file as a copy */
print_matrix(MATRIX1.matrix, MATRIX.rows, MATRIX.cols) ; /* prints this copyed matrix*/
當打印被複制的矩陣我得到一堆亂碼,海量數字(即1259000000000000000000)我認爲這些都是內存位置名稱或東西,有人可以幫我修理我的read_matrix()
函數?
非常感謝。
下面是我的完整代碼(它編譯)。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <malloc.h>
#include <conio.h>
double random() ;
double *rand_matrix(double *mat, int m, int n) ;
void print_matrix(double *mat, int m, int n) ;
void matrix_write(FILE *fptr, double *mat, int m, int n) ;
double *matrix_read(FILE *fptr, double *mat, int m, int n) ;
int main()
{
struct matrep {
unsigned rows,cols;
double *matrix;
} MATRIX,MATRIX1,MATRIX2;
int check = 0 ;
FILE *fptr;
printf("\n\nMatrix Manipulations Program");
do {
printf("\n\nEnter matrix dimensions : rows x columns : ");
check = scanf("%d x %d", &MATRIX.rows, &MATRIX.cols);
_flushall();
} while (check != 2 || MATRIX.rows < 1 || MATRIX.cols < 1) ;
MATRIX.matrix = (double *) malloc(sizeof(double) * MATRIX.rows * MATRIX.cols) ;
if (!MATRIX.matrix){
printf("\n\nSTOP : unable to allocate memory - exiting program") ;
exit(1) ;
}
rand_matrix(MATRIX.matrix, MATRIX.rows, MATRIX.cols); /* populates matrix with random data */
print_matrix(MATRIX.matrix, MATRIX.rows, MATRIX.cols) ; /* prints the matrix */
matrix_write(fptr, MATRIX.matrix, MATRIX.rows, MATRIX.cols) ; /* writes matrix to file*/
_flushall();
getchar();
MATRIX1.matrix=matrix_read(fptr, MATRIX.matrix, MATRIX.rows, MATRIX.cols) ; /* reads above matrix from file as a copy */
print_matrix(MATRIX1.matrix, MATRIX.rows, MATRIX.cols) ; /* prints this copyed matrix*/
_flushall();
getchar();
}
/***********************************************************/
double random()
{
static int seeded = 0;
double val ;
if (!seeded)
{
srand(time(NULL)) ;
seeded = 1;
}
val = ((double)rand())/ (double)RAND_MAX * 100.0 ;
return val ;
}
/***********************************************************/
double *rand_matrix(double *mat, int m, int n)
{
double *ptr ;
int i, j ;
ptr = mat ;
for (i=0; i < m; i++){
for (j=0; j < n; j++){
*ptr++ = random() ;
}
}
return mat ;
}
/***********************************************************/
void print_matrix(double *mat, int m, int n)
{
double *ptr ;
int i, j ;
if (mat==0 || m==0 || n==0)
{
printf("\n\nEmpty matrix");
return ;
}
ptr = mat ;
printf("\n\nrows %d, columns %d\n\n", m, n) ;
for (i=0; i < m; i++)
{
for (j=0; j < n; j++)
{
printf("\t%5.2lf", *ptr++);
}
printf("\n") ;
}
}
/***********************************************************/
void matrix_write(FILE *fptr,double *mat, int m, int n) {
int i,j;
if((fptr=fopen("matrixA.txt","w"))==NULL)
{
puts("Cannot Open File");
}
fprintf(fptr,"\n\nrows %d, columns %d\n\n", m, n) ;
for (i=0; i < m; i++)
{
for (j=0; j < n; j++)
{
fprintf(fptr, " %5.2lf", *mat++);
}
fprintf(fptr, "\n") ;
}
fclose(fptr);
}
/***********************************************************/
double *matrix_read(FILE *fptr,double *mat, int m, int n) {
int i,j;
double *ptr,x ;
ptr=mat;
if((fptr=fopen("matrixA.txt","r"))==NULL)
{
puts("Cannot Open File");
}
rewind(fptr);
fscanf(fptr,"\n\nrows %d, columns %d\n\n", &m, &n) ;
mat = (double *) malloc(sizeof(double) * m * n) ;
for (i=0; i < m; i++)
{
for (j=0; j < n; j++)
{
fscanf(fptr, " %5.2lf", &x);
*pt++r=x;
}
}
fclose(fptr);
return mat ;
}
'* pt ++ r = x;'in'matrix_read()'? – 2011-12-28 18:05:45
注意:在程序結束時,你必須free()''malloc()''ed內存! – 2011-12-28 18:09:34