Click to See Complete Forum and Search --> : Storing Tables in Files


May 20th, 1999, 02:33 AM
Hi,

can anyone recommend an efficient method for storing a tab or comma delimited table in a file. Preferably I would like to save the file as a DAT file. Any suggestions would be greatly appreciated,

Daniel

Jason Teagle
May 20th, 1999, 03:20 AM
When you say DAT file, do you mean human-readable? If so, use CStdioFile and use its ReadString() and WriteString() methods to manipulate each line of the table (or each element, if you prefer). If you don't need it readable, you can Serialize() the table by making a CString of each line (or element) and using the insert operators (<< and >>) to serialize each CString.

Is this what you wanted to know?

Dave Lorde
May 20th, 1999, 06:33 AM
If you put your lines of table data into a standard library vector or list, it's easy:

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

vector<string> myTable; // array of strings for table

// fill myTable with strings here

ofstream outFile("MyFile.DAT"); // Open output file stream

// Copy myTable to outFile
copy(myTable.begin(), myTable.end(), ostream_iterator<string>(outFile));

if (!outFile.good())
{
// Handle errors here
}



Dave