Database Problem (Should be Simple)

Hi everyone. I have little 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?

 
_handle.open(fileName_, std::ios::in | std::ios::out | std::ios::app)


2) and the handler function

1
2
3
4
5
6
7
8
9
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.


Last edited on
What type is _handle?

I have little idea how databases are usually done, but I figured I'd try to write an advanced I/O database


The two parts of the sentence contradict each other. Writing an advanced database is not a trivial task, even if you knew how databases work. And if you knew, you wouldn't use iostream for that.

If you really want to write a database, first read some books on the topic (e.g. the classic one by Ullman) then study some code, then try to work on some existing engine and after that, if you still want to do that (I'm 99% sure you won't), you can write a database engine from scratch.
Last edited on
kbw:

_handle is an std::fstream object.

rapidcoder:

I don't have a computer science background (as you might guess), but rather engineering. I'm interested in expanding my C++ skills to the nth degree however because I like learning all types of stuff.

I bought the Ullman (1st ed.) book you referenced, as I had no idea how to even approach database systems. I found it on Amazon for $.54 + shipping. However complicated making a database system it is, I'm sick of the typical IO taught in 1st semester C++ courses of opening a text file, reading from/writing to it, and then closing the file. I want to learn much, much more general techniques for saving/loading data. Until the Ullman book ships to my house (2 weeks), do you have any resources/references/tips or paradigm shifts to offer as to why I can't write data to my text file? I am completely stuck. Should I even be using a text file?

Thanks for all your help guys.

Last edited on
1
2
3
4
5
6
int main(){
        Handler db;
        db.addText("writeline");
 
        return 0;
};

You are calling the default constructor, not the overloaded one that opens the file.
But haven't I overloaded the default constructor by giving my paramaterized constructor a default value?

1
2
3
4
5
Handler::Handler(cch* fileName_ = NULL){       
        if (fileName_ == NULL){
                fileName_ = "default.dat";
        };
        // ... 


Shouldn't it open "default.dat" ?
Usually the default is in the declaration not the definition.
Last edited on
I'm sorry, I don't understand. Why exactly won't the code I posted function as the default constructor?
It does, I initially didn't see the default since you had it in the definition. The problem is likely that you set the fileName to NULL then attempt to assign to it, try this Handler::Handler(cch* fileName_ = "default.dat")
Topic archived. No new replies allowed.