我正在編寫反彙編程序,並使用鏈接列表來保存符號文件(sym_file)中的數據。我一直在看所有其他的帖子,但仍然無法實現它的工作! (保持分段錯誤)我想追加列表末尾的節點,這樣我就可以跟蹤head_node。將節點添加到單個鏈接列表中
void read_symbol_file(char* file_name)
{
struct node* head_node = new struct node;
struct node* node = new struct node;
struct node* temp;
ifstream sym_file(file_name, ios::out);
char label[16];
char the_type[6];
char address[6];
if(sym_file.is_open())
{
while (!sym_file.eof())
{
sym_file >> label;
sym_file >> the_type;
sym_file >> address;
if(strcmp(the_type, "line") == 0)
{
node->type = line;
node->label = label;
node->address = atoi(address);
}
else if(strcmp(the_type, "block") == 0)
{
node->type = block;
node->label = label;
node->address = atoi(address);
}
else if(strcmp(the_type, "ascii") == 0)
{
node->type = ascii;
node->label = label;
node->address = atoi(address);
}
else if(strcmp(the_type, "word") == 0)
{
node->type = word;
node->label = label;
node->address = atoi(address);
}
else
{
cout << "invalid label" << endl;
exit(0);
}
if(head_node == NULL)
{
head_node = node;
head_node->next = node;
}
else
{
temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = NULL;
}
}
sym_file.close();
}
else
{
cout << "File does not exist or could not be found." << endl;
exit(0);
}
cout << head_node->label << endl;
}
我希望我每次看到['while(!sym_file.eof())'在本網站上使用不正確](http://stackoverflow.com/q/5605125/78845)時有$ 1! – Johnsyweb 2013-03-03 11:24:33
「不斷收到分段錯誤」。這應該可以幫助你追蹤錯誤。通過調試器運行此代碼,以查看您訪問的內存不屬於您的位置。通過只做一件事來減少函數的大小(追加到鏈表是與讀取文件完全不同的操作)。如果你仍然卡住,請發佈[簡短,獨立,正確(可編譯),示例](http://sscce.org/),我們會盡力協助。 – Johnsyweb 2013-03-03 20:47:17