[C++] Database Handler (simple)

  • C/C++
  • Thread starter X89codered89X
  • Start date
  • Tags
    Database
In summary, the person is trying to create an advanced I/O database in c++ using a wrapper class around the standard fstream class to make it universal for storing various types of data. However, they are having issues with writing data to the file, even though they are able to successfully create, open, and close the file. They have tried various methods, including using the put() function and the << operator, but the file remains blank. They are seeking help on how to properly write data to the file.
  • #1
X89codered89X
154
2
Mod note: Instead of using HTML FONT and SIZE tags, just put [ code ] and [ /code ] tags around your code.
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:
_handle.open(fileName_, std::ios::in | std::ios::out | std::ios::app)

2) and the handler function

Code:
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.
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
I think this would work.
Code:
_handle << line << endl;
 
  • #3
I tried that and my IO file is still blank. I don't know if it's even the output line that wrong, because I think ALL of these methods should work. Something else must be wrong.
 

Related to [C++] Database Handler (simple)

1. What is a database handler in C++?

A database handler in C++ is a piece of code that allows the program to connect to and interact with a database. It is responsible for establishing a connection, executing queries, and handling the results from the database.

2. How do I create a database handler in C++?

To create a database handler in C++, you will need a library or API that supports database connections, such as ODBC or JDBC. You will also need to include the appropriate header files and configure the connection parameters, such as the database name, username, and password.

3. What are the common functions of a database handler in C++?

The common functions of a database handler in C++ include establishing a connection, executing queries, retrieving data from the database, and handling errors and exceptions. It may also have functions for creating, updating, and deleting data in the database.

4. How can I handle errors in a database handler in C++?

You can handle errors in a database handler in C++ by using exception handling and checking for error codes after each database operation. It is also important to have error messages and logging in place to help with debugging and troubleshooting.

5. Can I use a database handler in C++ for different types of databases?

Yes, you can use a database handler in C++ for different types of databases as long as you have the appropriate library or API for that database. For example, if you want to connect to a MySQL database, you will need to use a library that supports MySQL connections.

Similar threads

  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
6
Views
893
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
6
Views
8K
Replies
10
Views
970
  • Programming and Computer Science
Replies
8
Views
5K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
6
Views
940
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top