Welcome! Log In Create A New Profile

Advanced

Saving LinkLists

Posted by ShaunGVW 
Announcements Last Post
Announcement : Programming Students at UNISA School of Computing 06/19/2019 02:01PM
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Saving LinkLists
July 21, 2006 01:33PM
Hi

Just a question.
I am wanting to write a program, and I'm thinking that using LinkLists will be the best way to accomplish it. How do I go about saving the whole LinkList into a file for later retrieval?

Shaun
Re: Saving LinkLists
July 24, 2006 06:56PM
Hi Shaun,

Well, I think you don't necessarily have to save the linked list to a file but only the data that it writes.

The procedure is still the same like you were saving your data through any means. The difference is not on storage/saving the data but the implementation of the program that outputs it of which the computer handles that. You now simply have to direct your output operations to a file rather than to the standard output.

I hope that answers your question.

Cheers,

Shupi.
avatar Re: Saving LinkLists
July 24, 2006 11:02PM
Like SKatsande said, you only need to save and restore the data held by each node of your linked list. However, it's not quite as simple as writing to the console output.

You have to be able to read it back from the disk and put it into memory in the same way as it was before saving to disk. Writing to file stream in the same way you write to the console is wasteful. You will end up with integers that could take up over 10 bytes each - they should only take 4 in a 32-bit system. You should open your file streams in binary mode and then use the read and write methods of the file stream objects.

Your biggest difficulty will be in saving strings. The best way to do this is to save the string length before the actual string so that when you want to read it out of the file, you know how many bytes to read. Apart from that, all classes and structs eventually decompose into basic data types (char, int, long, bool, etc.) so you shouldn't have too much problem. Just remember the format in which you saved the data and then read it back in that same format.

#include <fstream>
#include <string>
using namespace std;

// you can't have both of these instances at the same time but this 
// is how you'd define them.
ofstream fout("DataFile.dat", ios_base:: out | ios_base::binary);
ifstream fin("DataFile.dat", ios_base::in | ios_base::binary);

string const FileWriteError("Error writing to file.");
string const FileReadError("Error reading from file.");
string const UnexpectedEOF("Error: unexpected end of file");

void SaveInt(int const &value)
  {
  fout.write(reinterpret_cast<char *> (&value), sizeof(int));
  if (fout.bad())
    throw FileWriteError;
  }

void SaveString(string const &str)
  {
  int length = str.size();

  SaveInt(length);
  fout.write(str.c_str(), length);
  if (fout.bad())
    throw FileWriteError;
  }

int RestoreInt()
  {
  if (fin.eof())
    throw UnexpectedEOF;

  int value;
  
  fin.read(reinterpret_cast<char *> (&value), sizeof(int));
  if (fin.bad())
    throw FileReadError;

  return value;
  }

string RestoreString()
  {
  int length = RestoreInt();

  if (fin.eof())
    throw UnexpectedEOF;

  string str;
  str.resize(length);
  
  fin.read(&str[[color=red]0[/color]], length);
  
  if (fin.bad())
    throw FileReadError;
  
  return str;
  }
These are just examples of how to read and write integers and strings to a file in binary mode. You can create functions for all the basic types using the same algorithms.

A better way would be to encapsulate all the reading functions into a reading class and all the writing functions into a writing class. You supply the file name to the constructor and then call the individual functions to read the data from file. You open the file in the constructor and you close it in the destructor (so you don't have to worry whether it's closed or not - it becomes automatic). You keep the file stream as the only data member of each class.
Re: Saving LinkLists
July 25, 2006 07:03AM
Thank you. I appreciate the effort and time you've given my question. I will see what I can make from your code you supplied.

Shaun
Sorry, only registered users may post in this forum.

Click here to login