声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 2256|回复: 5

[C/C++] 如何将TXT内容转换为数组啊!!!!

[复制链接]
发表于 2010-4-7 13:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
我有一个256*256的矩阵,是TXT格式,用C编程时要将矩阵读入并存入二维数组dist中,以便程序进行,如何实现啊?哪位大哥帮帮小妹吧!!

[ 本帖最后由 程门映雪 于 2010-4-7 13:36 编辑 ]
回复
分享到:

使用道具 举报

发表于 2010-4-8 21:58 | 显示全部楼层
使用函数fscanf
发表于 2010-4-23 16:10 | 显示全部楼层
本帖最后由 Rainyboy 于 2010-10-18 15:47 编辑
  1. //给你一个我写好的例子


  2. #include<stdio.h>
  3. #include<math.h>
  4. #include<stdlib.h>



  5. int main(int argc,char *argv[])
  6. {
  7.      
  8.      int      row,column;
  9.      float    **mat;
  10.      FILE     *fin,*fout;
  11.      char *infile,*outfile;
  12.      char ifn[256],ofn[256];
  13.      infile=ifn;
  14.      outfile=ofn;
  15.      int i,j;
  16.      
  17.      if (argc>3){
  18.         printf("You give to many parameters!");
  19.         return -1;
  20.      }
  21.      else if (argc==1){
  22.              printf("enter sourse file,please\n");
  23.              scanf("%s",infile);
  24.              printf("enter a file for result,please\n");
  25.              scanf("%s",outfile);
  26.            }
  27.            else if (argc==2){
  28.                    infile=argv[1];
  29.                    printf("enter a file for result,please\n");
  30.                    scanf("%s",outfile);
  31.                  }
  32.                  else {
  33.                        infile=argv[1];
  34.                        outfile=argv[2];
  35.                  }
  36.      //输入矩阵的行数和列数
  37.      printf("enter the rows of the matrix,please:\n");
  38.      scanf("%d",&row);
  39.      printf("enter the columns of the matrix,please:\n");
  40.      scanf("%d",&column);
  41.      if (row!=column){
  42.         printf("this is not a squire matrix!");
  43.         return -1;
  44.      }
  45.      
  46.      
  47.      //申请空间
  48.      mat = (float **)malloc(sizeof(float*)*row);
  49.      
  50.      if (0 == mat){                           // 内存申请失败,提示退出
  51.         printf("out of memory,press any key to exit...\n");
  52.         fprintf(fout,"out of memory,press any key to exit...\n");
  53.         exit(0);             // 终止程序运行,返回操作系统
  54.      }
  55.      //申请动态数组列动态申请
  56.      for (i=0;i<row;i++)
  57.          *(mat+i)=(float *)malloc(sizeof(float)* row);
  58.      if ((fin=fopen(infile,"r"))==NULL){        //打开源文件
  59.         printf("cannot open infile\n");
  60.         exit(0);
  61.      }
  62.       if ((fout=fopen(outfile,"w"))==NULL){     //打开要保存结果的文件
  63.         printf("cannot open outfile\n");
  64.         exit(0);
  65.      }
  66.      
  67.      
  68.      
  69.      
  70.      //mat从sourse 读取数据
  71.      for (i=0;i<row;i++){
  72.          for (j=0;j<row;j++)
  73.              fscanf(fin,"%f", &mat[j]);
  74.      }
  75.      
  76.      //输出读到得矩阵
  77.      printf("the origin matrix:\n");
  78.      fprintf(fout,"the origin matrix:\n");
  79.      for (i=0;i<row;i++){
  80.          for (j=0;j<row;j++){
  81.              printf("%16f",mat[j]);
  82.              fprintf(fout,"%16f", mat[j]);
  83.          }
  84.          printf("\n");
  85.          fprintf(fout,"\n");
  86.      }
  87.      
  88.      
  89.      //养成好习惯
  90.      free(mat);
  91.      fclose(fin);
  92.      fclose(fout);
  93. }
复制代码

评分

1

查看全部评分

发表于 2010-4-23 16:12 | 显示全部楼层
你先试一下吧,不行再说
发表于 2010-4-27 19:12 | 显示全部楼层
不明白楼主的意思,
fortran直接open一个文件,再读入到数组中即可的,
或matlab中直接load即可

有甚么困难么
发表于 2010-5-22 16:40 | 显示全部楼层

回复 5楼 linqus 的帖子

我记得 FORTRAN 只能读64个字长 好像。。
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-26 16:44 , Processed in 0.061004 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表