我有一個循環,執行以下操作:我不明白爲什麼地圖事業分割故障
short fooID;
char line[256]
map<short,foo> foos;
set<short> barIDs;
while (fgets(line,256,file) != NULL){
string line_copy = line;
/*use token to split the line into several parameters which does not effect foo * bar*/
string token = strtok(line,",");
token = strtok(NULL,",");
token = strtok(NULL,",");
token = strtok(NULL,",");
token = strtok(NULL,",");
barID = short(atoi(token));
foo * bar;
bar = new foo;
if(barIDs.find(barID) == barIDs.end()){
barIDs.insert(barID);
bar->setID(barID);
this->foos[barID] = bar;
}
}
當我運行這段代碼,我得到一個分段錯誤時,所有的酒吧都從文件加載。 barID範圍是1-1192。
有什麼想法?
感謝
上面的代碼只是我的實際代碼
展'線'的聲明。 – 2011-03-22 09:35:57
你顯示的代碼部分似乎是無辜的(模緩衝區溢出)。請顯示更多代碼。 – Vlad 2011-03-22 09:39:28
你能夠運行這個代碼!?你遺漏了一個分號,你的fgets()是錯誤的,你沒有* /來關閉註釋。 – quasiverse 2011-03-22 09:39:41