Saiyon
2010-04-05 17:54:47 UTC
This is what I have so far: It will compile, but the program crashes after going through the first number. Furthermore, I haven't gotten to sorting my linked list yet.
// reading a text file
#include
#include
#include
using namespace std;
int data = 0;
int info = 0;
int node_number = 1;
typedef struct node
{
string line; // will store information
node *next; // the reference to the next node
};
int main () {
string line;
node *head = NULL;
node *temp;
temp = (node*)malloc(sizeof(node));
temp->line = info; // store data(first field)
temp->next=head; // store the address of the pointer head(second field)
head = temp; // transfer the address of 'temp' to 'head'
ifstream myfile ("test.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
node *temp1; // create a temporary node
temp1 = (node*)malloc(sizeof(node)); // allocate space for node
temp1 = head;
for( int i = 1 ; i < node_number ; i++ )
{
temp1 = temp1->next; // go to the next node
if( temp1 == NULL )
{
cout<
}
}
node *temp; // create a temporary node
temp = (node*)malloc(sizeof(node)); // allocate space for node
temp->line = info; // store data(first field)
temp->next = temp1->next; //transfer the address of temp1->next to temp->next
temp1->next = temp; //transfer the address of temp to temp1->next
node_number++;
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Help is appreciated. Feel free to ignore my approach. 10 points to whoever gets me to a working program.