[問題] 將txt檔讀至二維陣列

看板C_and_CPP (C/C++)作者 (謬個小拉)時間16年前 (2009/10/05 16:50), 編輯推噓1(105)
留言6則, 1人參與, 最新討論串1/1
以爬過文 但找不到我要的答案 想請問將.txt或是.csv讀入二維陣列的方法 之前試著用過TString處理 但運算速度很慢 以下是我新寫的方法 會出現can't convert 'char *' to 'char' 的錯誤 想請問該如何解決呢? 或是前輩們有什麼可快速存取陣列的方法? 謝謝 void __fastcall TForm1::Button1Click(TObject *Sender) { int ii,i,j,k; TDateTime STime,ETime; STime=Now(); //--------------------------------------- char **RNInfo; RNInfo=new char*[368]; for (i=0; i < 368; i++) RNInfo[i]=new char[8]; string str; char *atemp,*a; ifstream ptr; ptr.open("E:\\RoadNodeInfo.txt",ios::in); i=0; while(getline(ptr,str,'\n')){ atemp=strdup(str.c_str()); a = strtok(atemp," "); j=0; while(a){ RNInfo[i][j]=a; j++; a=strtok(NULL," "); } i++; } ptr.close(); //--------------------------------------- ETime=Now(); Memo1->Lines->Append(STime.DateTimeString()+" "+ETime.DateTimeString()); } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.57.244.30

10/05 16:54, , 1F
RNInfo[i][j]是一個char, a是一個char*, 這裡會報error
10/05 16:54, 1F

10/05 16:55, , 2F
不是很正常嗎?? 要做char string copy請用strcpy(), 要
10/05 16:55, 2F

10/05 16:55, , 3F
逐char自己copyo, 你應該在while(a)裡為j另外寫個迴圈
10/05 16:55, 3F

10/05 16:55, , 4F
從a[]一個一個copy進RNInfo[i][]去.
10/05 16:55, 4F

10/05 16:56, , 5F
最後, 總覺得你的程式沒有很留心陣列的邊界判斷, 那你
10/05 16:56, 5F

10/05 16:57, , 6F
要自己確保輸入的資料大小的處理:)
10/05 16:57, 6F
文章代碼(AID): #1AoRCy-I (C_and_CPP)
文章代碼(AID): #1AoRCy-I (C_and_CPP)