[問題] c 讀檔的問題 fscanf
如果我已經 為一個動態陣列 申請memory的位置 令他為 G[][]
FILE *gtxt;
gtxt=fopen("g.txt","r");
for(int i=0;i<K;i++)
for(int j=0;j<N;j++)
{
fscanf(gtxt,"%d",&G[i][j]);
這樣讀出來的G會錯 假設我檔案只有0或者1 讀出來有幾行會變成
235222 0 0 0 0 這種大數字
int tt;
fscanf(gtxt,"%d",&tt);
G[i][j]=tt;
這樣就不會錯 為什麼呢 ??
}
幫解惑~~~ 感謝!
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.224.53.123
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1477327400.A.5F5.html
→
10/25 01:07, , 1F
10/25 01:07, 1F
推
10/25 01:12, , 2F
10/25 01:12, 2F
→
10/25 01:17, , 3F
10/25 01:17, 3F
→
10/25 01:27, , 4F
10/25 01:27, 4F
#define MALLOC2(type,name,len_1,len_2)
{
int i;
name=(type**)malloc(sizeof(type*)*len_1);
for( i=0; i<len_1; i++ )
name[i]=(type*)malloc(sizeof(type)*len_2);
}
申請:呼叫上面定義的 MALLOC2(int,G,K,N);
※ 編輯: es86016 (36.224.53.123), 10/25/2016 01:32:14
※ 編輯: es86016 (36.224.53.123), 10/25/2016 01:32:42
推
10/25 02:25, , 5F
10/25 02:25, 5F
→
10/25 07:40, , 6F
10/25 07:40, 6F
→
10/25 07:42, , 7F
10/25 07:42, 7F
→
10/25 08:07, , 8F
10/25 08:07, 8F
→
10/25 08:56, , 9F
10/25 08:56, 9F
→
10/25 13:53, , 10F
10/25 13:53, 10F
→
10/25 13:54, , 11F
10/25 13:54, 11F
→
10/25 15:21, , 12F
10/25 15:21, 12F