Converting file handling to linked list

Hi Sir,

I am new here. I need your help in converting below to linked list. Would you know how to do this? Thank you in advance

void read (){
ifstream in_File ("Student.txt");
if(!in_File.is_open()){
cout << "File failed to open" << endl;

}

string myString;
string line;

struct node *temp;
temp=head;
while (getline(in_File, line)){
stringstream ss (line);
getline(ss, myString, ',');
stu.student_number = atoi(myString.c_str());
getline(ss, stu.first_name, ',');
getline(ss, stu.middle_name, ',');
getline(ss, stu.last_name, ',');
cout << stu.student_number << endl;
cout << stu.first_name << endl;
cout << stu.middle_name << endl;
cout << stu.last_name << endl;
}

in_File.close();
}
This reads all the lines into a linked list.

1
2
3
4
5
6
7
8
9
10
11
12
13
ifstream in_File ("Student.txt");
if(!in_File.is_open())
{
  cout << "File failed to open" << endl;
  return;
}

list<string> list_of_strings; // the linked list
string line;
while (std::getline(in_File, line))
{
  list_of_strings.push_back(line);
}


hi repeater...

I think you are using other library for this? Do you have another way without using other library... below is an example of writing a linked list to a file.. But I am unable to do the same for reading a file and put ir in a linked list... Thanks in advance...

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
void writeinfile(){
	ofstream out_File ("Student.txt", ios::app);
	
	int i;
	
		struct node *temp;
		temp=head;
		if(temp==NULL)
	{
	   cout << "List is empty";
	}
	else
	{
		cout << "\n\t DATA IS BEING SAVED IN FILe\n";
		for(i=0;temp!=NULL;i++){
	  	out_File << temp->student_number << "," <<temp->first_name << "," 
		  <<temp->middle_name << "," <<temp->last_name << "," 
		  <<temp->gender << "," << temp->age << "," 
		  <<temp->birth_month<<"/"<< temp->birth_day<<"/"<<temp->birth_year<<","
		  <<temp->address<< "," << temp->contact_number<< ","
		  <<temp->email_add<< "," << temp->father_name<< ","
		  <<temp->mother_name<< "," << strProgram(temp->program) << endl;

		temp=temp->next;
		}//for
	cout << "\n\t----- DONE ----- DATA SAVED SUCCESSFULLY\n";
	}//else
	out_File.close();
}
list is part of c++.

reading in should just be along the lines of
tmp = new node;
file >> (*tmp).fields; //all the fields like above
insert tmp into list. Preferably at the top each time:
tmp->next= head;
head = tmp;
this reverses the data but its a lot less aggravation. if you feel compelled to insert at the end, make sure your list keeps a tail pointer and work it from there
tmp -> next = null;
tail -> next = tmp;
tail = tmp;
Last edited on
Thank you jonnin for the response… :) will try it out... do I need to replace my read function code or this is in addition to that code?
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
struct Node
{
    int data;         // for example

    Node *pNext; // pointer to next node
};

void foo()
{
    Node *linkedList = NULL;

    while (/* some condition */)
    {
        Node *node = new Node;
        int data;

        // Read data from file . . .

        // assign its value to the current node's data
        node->data = data;

        node->pNext = linkedList;    // this adds the current node 
        linkedList = node;                // to the beginning of the list 
    }

    // now you have a linked list which you could iterate through like this
    Node *iterator = linkedList;
    while( iterator )
    {
        std::cout << iterator->data << std::endl;
        iterator = iterator->pNext;
    }

    // don't forget to free the allocated memory
    Node *it = linkedList;
    Node *temp;
    while( it )
    {
        temp = it;
        it = it->pNext;
        delete temp;
    }
}
Last edited on
Topic archived. No new replies allowed.