作者hsiaotzu0911 ()
看板C_and_CPP
标题[问题] VC++ 执行到一半就出现存取违规被强制中断
时间Tue May 5 11:53:02 2009
於 0x1026642c (msvcr90d.dll) 的 guestbook.exe 中
发生未处理的例外状况: 0xC0000005: 读取位置 0x00000032 时发生存取违规
以下是程式码
char fname[1024] ;
sprintf(fname,"%s_GB.txt", userID.c_str());
ifstream file2("test.txt");//我已经先抓好的网页原始码
ofstream GB(fname);
char record2[1024];
char string1[] = "\"msg_img\""; //ID前的辨识字串
while(!file.eof()){
file.getline(record2,sizeof(record2));
if(strstr(record2,string1)){
while(!strstr(record2,"<script>")){
GB << strtok(strstr(record2,string1)+50,"\"");
file.getline(record2,sizeof(record2));
if(strstr(record2,"<script>"))
break;
else
GB<<endl;
}
break;
}
}
file.close();
GB.close();
大约是想把我在html里面的原始码撷取我要的字串存在txt档里面
我用cout去测试的结果是 第二个while回圈会正常跑一次
然後跑第二次的时候就停在GB << strtok.......那边发生问题 就强迫中断
想请问是什麽问题? 因为之前也有写个类似的程式码 但是没有问题
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.119.164.243
1F:→ jaw109:题外话, 虽然缩排是好习惯, 但缩得太深也不是件好事... 05/05 12:33
2F:→ jaw109:GB << strtok(strstr(record2,string1)+50,"\""); 这行.. 05/05 12:37
3F:→ jaw109:发生错误之前的那瞬间record2和string1里面是什麽东西? 05/05 12:38
4F:→ hsiaotzu0911:喔 歹事 我没有全部复制出来 record2是个char型态 05/05 12:39
5F:→ hsiaotzu0911:string1也是char型态 已经储存好我要比对的字串 05/05 12:40
6F:→ jaw109:要不要透露一下当时这两个变数的内容? 不然这样很难猜是哪 05/05 12:44
7F:→ hsiaotzu0911:好 那我把所有的程式码贴出来? 05/05 12:45
※ 编辑: hsiaotzu0911 来自: 140.119.164.243 (05/05 12:49)
8F:→ jaw109:里出错... 你可以在程式码里面加一些类似assert或trycatch 05/05 12:46
9F:→ jaw109:的东西帮忙debug 05/05 12:46
10F:推 ledia:有 "<script>" 就一定会有 "msg_img" 吗? 一定隔 50 bytes ? 05/05 13:58
11F:→ hsiaotzu0911:对 当判断到msg_img之後数50个开始读我要的东西 然後 05/05 14:14
12F:→ hsiaotzu0911:一定会有"可以停止 然後在所有的msg_img都读完之後 05/05 14:14
13F:→ hsiaotzu0911:就会接着有<script>的tag 可以break掉 让原始码提早 05/05 14:15
14F:→ hsiaotzu0911:结束 不用读到档案的底端 05/05 14:15