link list segmentation fault
Dec 8, 2010 at 1:17am UTC
I know this will be a simple and stupid error that I'm missing. Line 75 ends up pointing to something that the debugger can't show me, and then I get a segmentation fault on the next line.
This is reading in a file with a name and an ID. It's then creating a link list sorted on name. (There is another one to be generated on the ID, but I can sort that out later.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
using namespace std;
// delcare global variables
struct Node {
string name;
long ID;
Node * next;
};
string filename="a9.txt" ;
// prototype functions
Node * read (string);
int main()
{
// call functions
Node * entryPoint = read (filename); // call to read file, return linked list
return 0;
}
// read information from file
Node * read (string filename)
{
Node * current= new Node;
Node * head=current;
Node * readBuffer = new Node;
Node * pointer = new Node;
Node * temp1 = new Node;
Node * temp2 =new Node;
int count=0;
int i=0;
// int stringCompA=0;
// int stringCompB=0;
char IDstring[100];
ifstream fileRead;
fileRead.open (filename.c_str()); // open file for reading
fileRead >> readBuffer->name;
fileRead >> IDstring;
current->ID=atoi(IDstring);
fileRead >> pointer->name;
fileRead >> IDstring;
pointer->ID=atoi(IDstring);
// fileRead >> pointer->ID;
// stringCompA = strcmp(readBuffer->name,pointer->name);
if (readBuffer->name > pointer->name)
{
head = pointer;
current = readBuffer;
head->next = current;
}
else
{
head = readBuffer;
current = pointer;
head->next = current;
}
pointer = new Node;
current->next=pointer;
current = current->next;
current->next=NULL;
while (!fileRead.eof())
{
temp1=current;
temp2=temp1->next;
// temp2=current->next;
fileRead >> readBuffer->name;
fileRead >> readBuffer->ID;
// sort into name order
// temp1=current->next;
// stringCompA = strcmp(readBuffer->name,current->name);
// stringCompB = strcmp(readBuffer->name,temp1->name);
if (readBuffer->name < head->name)
{
temp1=head;
head=readBuffer;
head->next = temp1->next;
}
else if (readBuffer->name > current->name && readBuffer->name < temp2->name)
{
current->next = readBuffer;
readBuffer->next = temp2;
}
else if (readBuffer->name > current->name && current->next==NULL)
{
temp1=readBuffer;
current->next = temp1;
temp1->next=NULL;
}
temp1=new Node;
current->next = temp1;
temp1->next=NULL;
count++;
}
count--;
fileRead.close(); // close datafile
// output original file contents
cout << "File contents in original format: " << endl << endl;
current=head;
while (current->next!=NULL)
{
cout << current->name << " " << current->ID << endl;
current=current->next;
}
system ("Pause" );
system("cls" );
return head;
}
Thanks in advance.
Last edited on Dec 8, 2010 at 2:25am UTC
Dec 8, 2010 at 2:34pm UTC
A description of what you're trying to do along with a sample from the input file might help.
Topic archived. No new replies allowed.