Registered: 13 years ago
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?
Registered: 14 years ago
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.
Registered: 14 years ago
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));
void SaveString(string const &str)
int length = str.size();
fin.read(reinterpret_cast<char *> (&value), sizeof(int));
int length = RestoreInt();
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.