[問題] 為何最後會出現錯誤?

看板C_and_CPP (C/C++)作者 (乖乖)時間14年前 (2011/09/16 01:07), 編輯推噓1(1023)
留言24則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 為何程式run最後都會出現錯誤, 還需要我強迫終止? 餵入的資料(Input): test.txt檔的內容如下: This is the test file. a,b,c,d,e,f,g,h,i,j,k,l,m,no a00,b00,c00,d00, , e00,f00 預期的正確結果(Expected Output): 正確的將pch2中的每一個字串存放到storeTxt二維陣列中, 並且存放時情形如下 storeTxt[0][0]=This storeTxt[0][1]=is storeTxt[0][2]=the storeTxt[0][3]=test storeTxt[0][4]=file. storeTxt[1][0]=a storeTxt[1][1]=b storeTxt[1][2]=c . . . storeTxt[1][13]=no storeTxt[2][0]=a00 storeTxt[2][1]=b00 storeTxt[2][2]=c00 storeTxt[2][3]=d00 storeTxt[2][4]=e00 storeTxt[2][5]=f00 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <sstream> using namespace std; const int MAX_STR=256; int _tmain(int argc, _TCHAR* argv[]) { char **storeTxt=new char*[10000]; for(int i=0;i<10000;i++) { storeTxt[i]=new char[10000]; } int x,y; ifstream sFile; sFile.open("test.txt"); y=0; while(!sFile.eof()) { char str2[256]; sFile>>str2; char * pch2; pch2=strtok(str2," ,"); x=0; while(pch2 != NULL) { cout<<pch2<<endl; storeTxt[y] = pch2; pch2=strtok(NULL, " ,"); x++; } y++; } sFile.close(); for(int j=0;j<10000;j++) { delete [] storeTxt[j]; } delete [] storeTxt; system("PAUSE"); return 0; } 補充說明(Supplement): 問題應該是出現在內層的while迴圈的 storeTxt[y] = pch2; 我實在是不知道如何修改, 請各位大大指教. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 1.169.168.111 ※ 編輯: bodboy 來自: 1.169.168.111 (09/16 01:19)

09/16 01:10, , 1F
讀檔判斷不要用eof() → while( sFile >> str2 )
09/16 01:10, 1F

09/16 01:11, , 2F
想知道錯誤也先確定是不是 new 失敗, 中間檢查程式碼
09/16 01:11, 2F

09/16 01:12, , 3F
不能少, 也要自己找出是否檔案沒開成功
09/16 01:12, 3F

09/16 01:17, , 4F
storeTxt[y] = pch2; ← 你確定你知道這在做什麼嗎?
09/16 01:17, 4F

09/16 01:17, , 5F
storeTxt[y] = pch2; ----> strcpy ??
09/16 01:17, 5F

09/16 01:22, , 6F
我覺得這直接用 vector+string+stringstream 較適合耶.
09/16 01:22, 6F

09/16 01:23, , 7F
要用char** 讀的話,那應該還要再配 int* 記錄每行個數.
09/16 01:23, 7F

09/16 01:37, , 8F
多一個nullptr當sentinel即可
09/16 01:37, 8F

09/16 01:40, , 9F
當 sentinel 的話,不就把原本 new 出來的指走,產生
09/16 01:40, 9F

09/16 01:40, , 10F
memory leak 嗎?還是我誤會了些什麼 XD
09/16 01:40, 10F

09/16 01:43, , 11F
不過想想..用stringstream取代strtok似乎麻煩些倒是..
09/16 01:43, 11F

09/16 02:03, , 12F
09/16 02:03, 12F

09/16 02:12, , 13F
我真的是不會 c++ 啊 .. orz
09/16 02:12, 13F

09/16 02:13, , 14F
提示: 當讀入的字串為 "This is the test file.", 表
09/16 02:13, 14F

09/16 02:14, , 15F
示你至少得為它保留 23 個字元的空間(懶人作法的話),
09/16 02:14, 15F

09/16 02:16, , 16F
這樣storeTxt[0][0]的形態就會是char*而不是char, 你
09/16 02:16, 16F

09/16 02:16, , 17F
不只型態都有問題, 記憶體也配得不夠
09/16 02:16, 17F

09/16 09:13, , 18F
~"~不喜歡這樣的標題
09/16 09:13, 18F

09/16 09:18, , 19F
x++;<--? y++? strcpy ?如果你的第一維代表行的話
09/16 09:18, 19F

09/16 09:21, , 20F
第二維是代表行中的word的話
09/16 09:21, 20F

09/16 09:22, , 21F
string **storeTxt=new string*[10000];
09/16 09:22, 21F

09/16 09:22, , 22F
storeTxt[i]=new string[10000];
09/16 09:22, 22F

09/16 09:23, , 23F
storeTxt[y][x] = pch2; 應該是要這樣修改.
09/16 09:23, 23F

09/16 09:26, , 24F
很少人敢說自己會c++.
09/16 09:26, 24F
文章代碼(AID): #1ESZ382a (C_and_CPP)
文章代碼(AID): #1ESZ382a (C_and_CPP)