views:

774

answers:

7

I am making an application that simulates an ATM (its totally trivial). I was having a little trouble saving my transactions to the hard disk. The two main questions are: A) Should I save it as a DB or a text-file, B) How would I save to disk using either DB or txt format in STL C++ (I don't really want to use a third-party library but I will if necessary).

Thanks in advance :-)

+6  A: 

something like this:

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

int main () {
  ofstream myfile;
  myfile.open ("example.txt");
  myfile << "Writing this to a file.\n";
  myfile.close();
  return 0;
}

However, you may want to look in to linking SQLite to your application, since it is the most widely distributed database in the world: http://www.sqlite.org/capi3.html

It is solid, and is used my many mobile devices, and Thunderbird and Firefox.

John Gietzen
You can pass the filename as a parameter to the ctor and the close() call is superfluous.
dirkgently
Don't close the file. You wouldn't resize a vector to 0 before returning, would you?
MighMoS
@MighMoS: Do the ofstream objects clean themselves up nicely on destruction without a close then?I think I've always been in the habit of closing files after usage to explicitly say "This is where I am done with the file" - probably a habit from some older languages that didn't manage the memory correctly and left the file open.
Andy
Ah yes, I forgot that C++ will call the destructor. I kinda got lost in the world of late garbage collection.
John Gietzen
+1  A: 

A) Should I save it as a DB or a text-file,

That will depend on the usage/application -- you are the best judge.

B) How would I save to disk using either DB or txt format in STL C++ (I don't really want to use a third-party library but I will if necessary).

STL does not provide you with a a database driver. It does allow you to create output streams and save data as files-on-disk. If you plan to write out a DB-file, you need to know about the internal format of the DB and write a binary file.

dirkgently
+1  A: 

Text files are easy - <fstream> has the class you'll need - ofstream. Override operator << for your transaction class (you are using a transaction class, right?), then just write to the ofstream like you'd write to cout.

Harper Shelby
I'm using a class. Good guess ;-)
Lucas McCoy
A: 

For C++ you may want to take a look at fstream.

If you can use an external (but very standard) library, boost's serialization may be of great help to you -- especially if you're looking to dump the contents of STL containers (not obvious if this is what you want to do, from your question).

Even if you can't afford an external library, I'd highly recommend looking at how serialization's api is designed.

leander
+1  A: 

If you are reasonably new to C++, I'd suggest just logging transactions out to a text file. The easiest way to do this in C++ is to use an ofstream like so:

#include <fstream>

void write_transaction(ofstream& file, transaction t)
{
   file << t.somedata;
}

int main(int argc, char** argv)
{
   ofstream file;
   file.open("transactions.txt");
   transaction t;
   t.data = "Something";
   write_transaction(file, t);
   file.close();
   return 0;
}

Of course, transaction is defined as something that contains all your transaction data and would probably be held in an array. write_transaction would then write out all the important data.

workmad3
+2  A: 

A database for this seems over kill just write one line to the file per transaction. A useful reference for the C++ streaming classes can be found here http://www.cppreference.com/wiki/io/start - which also provides just a good general reference for C++

Mike Lowen
Nice website. Thanks!
Lucas McCoy
A: 

i think it mainly depend on the puropose of your project. if it is something serious, it is better to use a DB..other wise file is better

actually some time i just use a file based db...