go away spammer

Author Topic: [C++] Database Handler (simple)  (Read 3198 times)

0 Members and 1 Guest are viewing this topic.

Offline jim5192Topic starter

  • Jr. Member
  • **
  • Posts: 18
  • Helpful? 0
  • "Brains!"
[C++] Database Handler (simple)
« on: October 18, 2012, 03:40:22 PM »
Hi everyone. I have no idea how databases are usually done, but I figured I'd try to write an advanced I/O database in c++, but I wanted to make a database class that's fairly universal for me to store raw data, text, lists, filepaths, and anything else I might need very easily.... and be able to edit it that information flexibly during runtime. My idea was to make some wrapper classes around the standard fstream class, but I'm getting stuck on something simple. Through my wrapper class (which at this point isn't much other than using fstream directly), I can successfully create a text file, open, and close it, but for whatever reason, I can't write any data to the file. I don't get any errors. The single I/O file is just blank. Any ideas?

My hunch was that c++ doesn't like fstreams that are both Input AND output, but the documentation on cplusplus.com seems to suggest this is done as often as just using input or output mode alone.

I'll post the relevant code in this pastebin: http://pastebin.com/06ejZdKd


I tend to code things I think I will need later and less on the problem at hand. Thus I have almost 1k lines (most of which aren't shown in link) and no working, functional database at all. I'll highlight what sections I was looking at to find the problem, but to no avail:

1) if you look at the declaration of the IO file, is this what I want?

Code: [Select]
_handle.open(fileName_, std::ios::in | std::ios::out | std::ios::app)
2) and the handler function

Code: [Select]
bool Handler::addText(cch* line_){
        if (_handle.is_open()){
                std::cout << "File Open!" << std::endl;
                _handle.put('s');
        } else {
                std::cout << "File Not Open!" << std::endl;
        };
 return true;
};

It may help to know that cch is a macro for const char.

How exactly should I write data to the file? Using put('s') makes no sense and was just for debugging purposes... but nothing worked that I tried. What should I have here?

Let me know what you think, and thanks in advance everyone.

NOTE: This is NOT a school assignment.

"Imagination is more important than knowledge."-Einstein

Offline newInRobotics

  • Supreme Robot
  • *****
  • Posts: 1,015
  • Helpful? 48
  • N.I.R.
Re: [C++] Database Handler (simple)
« Reply #1 on: October 19, 2012, 02:25:52 AM »
NOTE: This is NOT a school assignment.
Even if it is, what exactly does it have to do with robots? ;D In my opinion You'd get answer much quicker if You used C++ Forum :)
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W

Offline jim5192Topic starter

  • Jr. Member
  • **
  • Posts: 18
  • Helpful? 0
  • "Brains!"
Re: [C++] Database Handler (simple)
« Reply #2 on: October 19, 2012, 11:38:41 AM »
Didn't think of using that forum! Thanks! Although how couldn't this be used for robots? First robot I make will probably need some sort of a brain. This would be a great way to stored "learned" material. Just a thought.

Thanks though.
"Imagination is more important than knowledge."-Einstein

Offline newInRobotics

  • Supreme Robot
  • *****
  • Posts: 1,015
  • Helpful? 48
  • N.I.R.
Re: [C++] Database Handler (simple)
« Reply #3 on: October 19, 2012, 01:04:03 PM »
First robot I make will probably need some sort of a brain. This would be a great way to stored "learned" material.
No offence, but first (second, third and most likely fourth) robot You'll make will hardly have learning capability. To implement learning one has to understand at least something like Artificial Neural Networks. Another thing, if You decide to go "learning robot which uses database to store values" way, You'll most likely want to use something that is already available (database-wise), as database engines out there are fast and easy to use, as opposed to "home brewed" one which will be slow, difficult and time consuming to develop.

On the other hand, if You want to write database engine for the sake of learning how it all works, then go probably it will be useful for You to know that database files are binary files and not text files. Probably good starting point would be to find out how something like SQL database works.

Good luck :)
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W

Offline jim5192Topic starter

  • Jr. Member
  • **
  • Posts: 18
  • Helpful? 0
  • "Brains!"
Re: [C++] Database Handler (simple)
« Reply #4 on: October 19, 2012, 01:22:27 PM »
well I just happen to have a deep fascination with ANN's! Math is something I'm comfortable with.

What I'm not comfortable with is hardware...but this is interesting. So databases are programmed in SQL? is that something I can integrate into my program in C++? I'm using C++ because it's fast (and I don't want to rewrite useful tools I've made in a new language I don't know very well).

Thanks for your help!
« Last Edit: October 19, 2012, 01:27:07 PM by jim5192 »
"Imagination is more important than knowledge."-Einstein

Offline newInRobotics

  • Supreme Robot
  • *****
  • Posts: 1,015
  • Helpful? 48
  • N.I.R.
Re: [C++] Database Handler (simple)
« Reply #5 on: October 21, 2012, 04:21:29 AM »
So databases are programmed in SQL? [...] I'm using C++ because it's fast
SQL stands for Structured Query Language, as it implies, SQL is used to control contents of SQL database. Database engine itself is not written in SQL, as SQL is not fit for writing applications. Database engine can be written using any programming language, C++, C, Java, .Net to name a few. C++ is good, if You know how to use it. If one is beginner C++ programmer, software one writes can have many bugs and leaks making execution slow. All this can be avoided by using something like C#, where code is managed for You, hence avoiding many issues. My point is, just because You program in C++, it doesn't necessarily mean that Your code will execute faster than code of another language.

is that something I can integrate into my program in C++?
For well known database (MySQL, MS Access, Oracle, etc.) engines out there You can make SQL calls from many languages, that is the main point of a database.
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W

 


Get Your Ad Here