Thread: Saving and Loading Linked Lists With File i/o

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    13

    Saving and Loading Linked Lists With File i/o

    I am in desperate need of help here. I have been trying for weeks to get this program to work, and its no go. My program seems rather simple. Read in user info as a linked list, save it to a file, and then be able to choose one of those entries( Like a log in function) Now, Saving is easy. It saves fine this way...

    outfile << UserList->user.Fname;

    now, next, after all of the names and info have been stored in the binary file, I am supposed to be able to load it back into the program, 1 entry at a time, and choose an entry so I can put more info into that single entry. I am totally lost at this point, and have tried a huge amount of different ways to complete this part of my assignment. I even tried using a buffer and strcmp to search for partial matches. I would ask the teacher, but we ahve a sub for another 2 months who doesn't know the difference between a monitor and a toaster oven.

    any help would be GREATLY appreciated.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >doesn't know the difference between a monitor and a toaster oven.
    This describes most programming teachers.

    This sounds like a pretty straightforward assignment, read data from a file into a linked list and find a user input key to search for so that the data at the node can be updated. Perhaps you could be a bit more specific in what the problem you are having is?

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    13
    The error is simple. I don't know how to write the function that will read the linked list back into the program from the file.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The same way you would create a linked list from user input except you read the input from a file.
    Code:
    while ( in.good() ) {
      iter->next = new node;
      iter = iter->next;
      in>>iter->data;
      iter->next = NULL;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    13
    is "iter" a new list to load the info, or a previously entered list?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    It's a new list, you have to rebuild the links when you load the data from the file.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    13
    here is the relevant code from my program, I am just gonna post it here now because I do not have access from home.
    I'' try your suggestion, looks promising.
    Code:
    void Save(Users* &head)
    {
    	Users *savelist;
    	
    	savelist=head;
    	int i=0;
    	for(savelist=head;savelist;savelist=savelist=savelist->next)
    	{
    		//cout << savelist->Fname<<endl;
    		//cout << savelist->Sname<<endl;
    		i++;
    	//	getch(); 
    	}
    	/*FILE *fp;
    		if ((fp =fopen("D:\\Profiles.dat","r+b")) == NULL)
    			{
    			fp =fopen("D:\\Profiles.dat","w+b");
    		// error
    			//tprintf("Creating File, retry to create user");
    			getch();
    			}
    		
    	savelist=head;
    	while (savelist!=NULL)
    	{
    		cout <<"saving" <<endl;
    		fwrite(&savelist->Fname,sizeof(&savelist),1,fp);
    		savelist=savelist->next;
    	}
    	fclose(fp);*/
    	savelist=head;
    	ofstream outfile;
    	outfile.open("Profiles.dat",ios::out| ios::app |ios::binary);
    	//outfile <<head<<"\n";
    		//outfile<<"Begin" <<"\n";
    	do
    	{
    		outfile <<savelist->user.Fname <<"\n";
    		outfile <<savelist->user.Sname <<"\n";
    		outfile <<savelist->user.SepID <<"\n";
    		outfile <<savelist->user.UserID <<"\n";
    		
    		
    		
    		savelist=savelist->next; 
    	}
    	while(savelist!=NULL); 
    
    	outfile << "END";
    	outfile.close();
    	cout << "Save succesful"; 
    
      
    }
    void Login(int i,Users* &head)
    {
    	int profileID;
    	clrscr();
    	cout <<"Enter your student ID(if you have not created a profile with your user ID, you will need to return to the main menu)"<<endl;
    	cin>>profileID;
    	char ch ='\0';
    	int length;
      char * buffer;
    	i=0;
    	Users *newrec;//=head;
    	int endloop=1; 
    	ifstream readfile;
    	readfile.open("Profiles.dat",ios::in |ios::binary);
    	readfile.seekg((profileID-1)*sizeof(newrec),ios::beg);
    	readfile.read((char *) &newrec,sizeof(Users));
    	readfile >> newrec->user.Fname;
    	cout <<newrec->user.Fname;
    cout <<"done"<<endl;
    	getch();
    
    	/*readfile.seekg(ios::beg);
    	static char reader[10];
    	readfile.read(reader,endloop);
    	//cout <<reader<<endl;
    	do
    	{
    		//cout << "hello"<<endl;
    	//	getch();
    		//endloop++;
    	readfile.read(reader,endloop);
    	if(reader[10]=' ')
    	{
    
    //	cout <<reader<<endl;
    //	getch();
    	}
    	}while (endloop != 5);
    	
    */
    	readfile.close();
    	cout << "Load succesful";
    	
      
    getch();
    
    		
    }

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    13
    Originally posted by Prelude
    The same way you would create a linked list from user input except you read the input from a file.
    Code:
    while ( in.good() ) {
      iter->next = new node;
      iter = iter->next;
      in>>iter->data;
      iter->next = NULL;
    }
    -Prelude
    Crash error, Nothing specific.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    What the h---. I'm bored at work. No compiler to make sure this works though.

    Code:
    #include <iostream.h>
    
    struct node
    {
      int num;
      node * next;
    }
    
    int main()
    {
      //declare a list
      node * head = NULL;
      node * curr;
      int i;
    //add new node to end of list each time.
    for(i = 0; i < 3; i++)
    {
      curr = head;
       //declare a new node to add to list
       node *newNode = new node;
       //initialize members of new node
       newNode->num = i * i;
       newNode->next = NULL;
    
       if(head == NULL)//this is first node in list
       {
          head = newNode;
        }
        else 
       {
          //find end of list
          while(curr->next != NULL)
          {
              curr = curr->next;
           }
            //and add new node there
           curr->next = newNode;
       }
    }
    
    //write list data to file top to bottom
    ofstream fout("listData.txt");
    curr = head;
    while(curr != NULL)
    {
       fout << curr->num << endl;
       curr = curr->next;
    }
    
    fout.close();
    
    //now read it back into a new list
    ifstream fin("listData.txt")
    node * newHead = NULL;
    while(!fin.eof())
    {
       curr = newHead;
       //declare a new node to hold file data
       node * newNode = new node;
       //read in file data one node's worth at a time
       fin >> newNode->num;
       newNode->next = NULL;
       
       //and add to end new list
       if(newHead == NULL)
       newHead = newNode;
       else
       {
         while(curr->next != NULL)
         {
            curr = curr->next;
          }
          curr->next = newNode;
       }
    }
    
    fin.close();
    
    //display new list
    curr = newHead;
    while(curr)
    {
       cout << curr->num << endl;
    }
    
    //delete memory from the lists
    curr = newHead;
    while(curr)
    {
       while(curr->next != NULL)
       {
          curr = curr->next;
        }
        delete curr;
        curr = newHead;
    }
    
    curr = head;
    while(curr)
    {
      while(curr->next != NULL)
      {
         curr = curr->next;
       }
       delete curr;
       curr = head;
    }
    
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Saving and Loading Files
    By renanmzmendes in forum C++ Programming
    Replies: 6
    Last Post: 03-15-2008, 08:46 AM
  2. Using a linked list globally and File I/O
    By Leftos in forum C Programming
    Replies: 46
    Last Post: 01-07-2008, 11:21 AM
  3. Major Problems with GetSaveFileName() and GetOpenFileName()
    By CodeHacker in forum Windows Programming
    Replies: 8
    Last Post: 07-12-2004, 11:05 AM
  4. Problem loading file...URGENT
    By khpuce in forum C Programming
    Replies: 4
    Last Post: 05-19-2003, 08:14 AM
  5. Speeding up file loading
    By PJYelton in forum C++ Programming
    Replies: 14
    Last Post: 01-06-2003, 05:19 PM