[問題] 為何最後會出現錯誤?
開發平台(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
09/16 01:10, 1F
→
09/16 01:11, , 2F
09/16 01:11, 2F
→
09/16 01:12, , 3F
09/16 01:12, 3F
→
09/16 01:17, , 4F
09/16 01:17, 4F
→
09/16 01:17, , 5F
09/16 01:17, 5F
→
09/16 01:22, , 6F
09/16 01:22, 6F
→
09/16 01:23, , 7F
09/16 01:23, 7F
→
09/16 01:37, , 8F
09/16 01:37, 8F
→
09/16 01:40, , 9F
09/16 01:40, 9F
→
09/16 01:40, , 10F
09/16 01:40, 10F
→
09/16 01:43, , 11F
09/16 01:43, 11F
→
09/16 02:03, , 12F
09/16 02:03, 12F
推
09/16 02:12, , 13F
09/16 02:12, 13F
→
09/16 02:13, , 14F
09/16 02:13, 14F
→
09/16 02:14, , 15F
09/16 02:14, 15F
→
09/16 02:16, , 16F
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
09/16 09:18, 19F
→
09/16 09:21, , 20F
09/16 09:21, 20F
→
09/16 09:22, , 21F
09/16 09:22, 21F
→
09/16 09:22, , 22F
09/16 09:22, 22F
→
09/16 09:23, , 23F
09/16 09:23, 23F
→
09/16 09:26, , 24F
09/16 09:26, 24F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章