我正在编写一个反汇编程序并使用链表来保存符号文件(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;
}