本帖最后由 Rainyboy 于 2010-10-18 15:47 编辑
- //给你一个我写好的例子
- #include<stdio.h>
- #include<math.h>
- #include<stdlib.h>
- int main(int argc,char *argv[])
- {
-
- int row,column;
- float **mat;
- FILE *fin,*fout;
- char *infile,*outfile;
- char ifn[256],ofn[256];
- infile=ifn;
- outfile=ofn;
- int i,j;
-
- if (argc>3){
- printf("You give to many parameters!");
- return -1;
- }
- else if (argc==1){
- printf("enter sourse file,please\n");
- scanf("%s",infile);
- printf("enter a file for result,please\n");
- scanf("%s",outfile);
- }
- else if (argc==2){
- infile=argv[1];
- printf("enter a file for result,please\n");
- scanf("%s",outfile);
- }
- else {
- infile=argv[1];
- outfile=argv[2];
- }
- //输入矩阵的行数和列数
- printf("enter the rows of the matrix,please:\n");
- scanf("%d",&row);
- printf("enter the columns of the matrix,please:\n");
- scanf("%d",&column);
- if (row!=column){
- printf("this is not a squire matrix!");
- return -1;
- }
-
-
- //申请空间
- mat = (float **)malloc(sizeof(float*)*row);
-
- if (0 == mat){ // 内存申请失败,提示退出
- printf("out of memory,press any key to exit...\n");
- fprintf(fout,"out of memory,press any key to exit...\n");
- exit(0); // 终止程序运行,返回操作系统
- }
- //申请动态数组列动态申请
- for (i=0;i<row;i++)
- *(mat+i)=(float *)malloc(sizeof(float)* row);
- if ((fin=fopen(infile,"r"))==NULL){ //打开源文件
- printf("cannot open infile\n");
- exit(0);
- }
- if ((fout=fopen(outfile,"w"))==NULL){ //打开要保存结果的文件
- printf("cannot open outfile\n");
- exit(0);
- }
-
-
-
-
- //mat从sourse 读取数据
- for (i=0;i<row;i++){
- for (j=0;j<row;j++)
- fscanf(fin,"%f", &mat[j]);
- }
-
- //输出读到得矩阵
- printf("the origin matrix:\n");
- fprintf(fout,"the origin matrix:\n");
- for (i=0;i<row;i++){
- for (j=0;j<row;j++){
- printf("%16f",mat[j]);
- fprintf(fout,"%16f", mat[j]);
- }
- printf("\n");
- fprintf(fout,"\n");
- }
-
-
- //养成好习惯
- free(mat);
- fclose(fin);
- fclose(fout);
- }
复制代码
|