[英]C: read a 2D complex array of a certain format from file
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]。 關於如何修改代碼的任何建議將不勝感激!
嘗試這個
fscanf(fptB, "(%d, %d)%*c", &B[i][j].re, &B[i][j].im);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.