Here's the code I have thus far:
file structs.h
struct LinkHdr
{
int rec_count;
std::string ftype;
};
struct Link
{
int id;
std::string title;
std::string desc;
std::string url;
bool is_private;
};
file write.cc
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
#include <libgen.h>
#include "structs.h"
int
main(int argc, char **argv)
{
using namespace std;
char * program = basename(argv[0]);
fstream ofile;
if (argc > 1)
{
LinkHdr header = {
1,
"Test",
};
Link bookmark = {
1,
"Slashdot",
"News for nerds, stuff that matters",
"http://www.slashdot.org/",
false,
};
string filename = argv[1];
ofile.open(filename.c_str(), ios_base::out | ios_base::binary);
if(!ofile.is_open())
{
cerr << program << ": Can't open " << filename << " for output." << endl;
exit(EXIT_FAILURE);
}
ofile.write( reinterpret_cast<char *>(&header), sizeof header);
ofile.write( reinterpret_cast<char *>(&bookmark), sizeof bookmark);
ofile.close();
}
else
{
cout << "Usage: " << program << " <filename>" << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
file read.cc
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <libgen.h>
#include "structs.h"
int
main(int argc, char **argv)
{
using namespace std;
char * program = basename(argv[0]);
ifstream ifile;
if (argc > 1)
{
LinkHdr header;
Link bookmark;
string filename = argv[1];
ifile.open(filename.c_str(), ios::in | ios::binary);
if(ifile.is_open())
{
ifile.seekg(0);
cout << "file: " << filename << endl;
while(ifile.read(reinterpret_cast<char *>(&header), sizeof header))
{
cout << "count: " << header.rec_count << "\n"
<< "type: " << header.ftype << endl;
break;
}
while(ifile.read( (char *) &bookmark, sizeof bookmark))
{
cout << "title: " << bookmark.title << "\n"
<< "description: " << bookmark.desc << "\n"
<< "url: " << bookmark.url << "\n"
<< "is_private: " << bookmark.is_private << endl;
}
ifile.close();
}
}
else
{
cout << "Usage: " << program << " <filename>" << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
A couple of things to note:
- I know it'd be easier to just use an SQLite3 db, like Firefox does. I just want to learn how to do it the bad old way.

- It works just fine when I use a simple data type, like char blah[400];
This post has been edited by ibbie: 28 June 2009 - 02:58 PM

New Topic/Question
Reply



MultiQuote






|