【问题标题】:C: read a 2D complex array of a certain format from fileC:从文件中读取一定格式的二维复数数组
【发布时间】:2013-07-09 21:19:44
【问题描述】:

一个 2x2 复数矩阵

  1+2i 3+4i
  5+6i 7+8i 

保存在mat.txt中,格式为

  (1, 2) (3, 4)
  (5, 6) (7, 8)

如何将其读取到 C 中的矩阵中,以便

  B[0][0].re = 1, B[0][0].im = 2 
  B[0][1].re = 3, B[0][1].im = 4
  B[1][0].re = 5, B[1][0].im = 6 
  B[1][1].re = 7, B[1][1].im = 8

?

我有

  fptB = fopen("mat.txt","r");
  for(i=0; i <2; i++){
    for (j=0; j<2; j++){
      fscanf(fptB, "(%d, %d)", &B[i][j].re, &B[i][j].im);
    }
  }
  fclose(fptB);

但它只读取 B[0][0]。任何有关如何修改代码的建议将不胜感激!

【问题讨论】:

    标签: c arrays matrix scanf complex-numbers


    【解决方案1】:

    试试这个

    fscanf(fptB, "(%d, %d)%*c", &B[i][j].re, &B[i][j].im);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 2016-01-05
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      • 2016-08-11
      相关资源
      最近更新 更多