readmatrix.c

来自「在MPI上实现的矩阵相乘并行计算的源程序。」· C语言 代码 · 共 48 行

C
48
字号
/*read a matrix from a file*/#include "stdio.h"#include  "stdlib.h"#define CHARSIZE sizeof(char)#define INTSIZE sizeof(int)int main(){ char *filename;  FILE *fd; /*file pointee to the file */  int i,j;  int row,column;  int value;  filename=(char *)malloc(CHARSIZE*128);if (filename==NULL){printf("CANNOT allocate space for filename\n");exit(0);}  printf("Read the content of a file\n");  printf("Filename:");  scanf("%s",filename);  fd=fopen(filename,"r"); /*open a file for read only and the stream is positioned at the beginning ofthe file*/if(fd==NULL) /*error in opening the file*/  {free(filename);printf("CANNOT open the file %s coorectly\n",filename);exit(0); /*exit the system*/ }/*if*/fread(&row,INTSIZE,1,fd);fread(&column,INTSIZE,1,fd);printf("Row=%d,Column=%d\n",row,column);for (i=0;i<row;i++) { for(j=0;j<column;j++)   { fread(&value,INTSIZE,1,fd);     printf("%d ",value);   }/*for j*/ printf("\n");}/*for i*/fclose(fd);/*close file pointer*/free(filename);/*free dynamic allocated space*/return(0);  }/*main*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?