I am writing a disassembler and am using a linked list to save the data from a symbol file(sym_file). I have been looking at all the other posts but still cannot get it to work! (keep getting segmentation fault) I am trying to append nodes at the the end of the list so I can keep track of the 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;
}