Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

dq9

macrumors newbie
Original poster
Mar 13, 2008
26
0
Macintosh HD/Users/dq9
Hi I am looking for a tutorial on making a simple command line database with C++. Something like an address book or something simple like that. I'm thinking of storing all the information in an external file, so that I can be saved/altered.

Basically I am looking to be able to:
  • Save and load data from an external file (.dat, .txt, etc...)
  • Search through that data
  • Display searched results

Any help will be greatly appreciated.
 
I program on the side and as a hobby. I am a beginner / intermediate programmer with basic to intermediate knowledge of C++. I have messed around with file I/O before, but not a whole lot.

I also think I should add that I'm not looking to connect to a MySQL database with this project. I want everything to be local. I did a google search that connecting to a MySQL database came up a lot.
 
Do you want to complete this one task and that will be that, or do you want to build on your knowledge so you can complete many tasks like this in the future?

-Lee
 
I program on the side and as a hobby. I am a beginner / intermediate programmer with basic to intermediate knowledge of C++. I have messed around with file I/O before, but not a whole lot.

I also think I should add that I'm not looking to connect to a MySQL database with this project. I want everything to be local. I did a google search that connecting to a MySQL database came up a lot.

Perhaps more specific a question that some of the others have asked. How did you learn the C++ you know. What books did you read or or courses did you take?

MySQL can definitely still be local, but you could use something else like BDB http://en.wikipedia.org/wiki/Berkeley_DB instead of rolling your own.

B
 
Do you want to complete this one task and that will be that, or do you want to build on your knowledge so you can complete many tasks like this in the future?

-Lee

I will definitely use it to expand my knowledge in the future.

Perhaps more specific a question that some of the others have asked. How did you learn the C++ you know. What books did you read or or courses did you take?

I'm entirely self taught. I learned from cprogramming.com mostly and also cplusplus.com


The program I am looking to create will be a command line address book with a name, phone number, and e-mail, field. That's what I have thought up so far. I want to save all this information into a .txt file (To make things simple for now).
 
From your somewhat terse descriptions I believe the C++ STL provides everything you need.

Your data files could consist of nothing more than strings with fields separated by some marker character literal.

Using strings, iostreams, stringstreams and either/or/both of map/vectors.

Discuss the data you'd like to track, read, store and search for and someone'll be more than glad to make further suggestions.
 
For an address book, I would create an address book entry class and an address book class. Your address book would hold a data structure inside and use methods to insert, search, remove entries and so on. The simplest way to do this is probably to just use a vector, it's enough unless you intend to store millions of entries IMO. You would need to read up on serialization to be able to come up with a way to write and read your data structure from disk.

This all assumes that the goal here is to create everything your self. Other wise look into SQLite, which gives you an in memory SQL data base.
 
I'm looking to search for, write, and read strings from a file.

I think this is where the sites you have been using fall apart as a learning tool, and I would recommend you check out some more structured resource like a book.

Maybe not something like the typical Strostrup http://www.amazon.com/C-Programming-Language-Special/dp/0201700735 but maybe something more like C++ Cookbook http://oreilly.com/catalog/9780596007614/ or Practical C++ Programming http://oreilly.com/catalog/9780596004194/ that already assume some knowledge.

I haven't actually read C++ Cookbook, but other O'Reilly Cookbooks have been very useful and I believe it introduces Boost and STL.

B
 
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.