[C++] Database Handler (simple)

  • Context: C/C++ 
  • Thread starter Thread starter X89codered89X
  • Start date Start date
  • Tags Tags
    Database
Click For Summary
SUMMARY

The discussion centers on creating a universal database handler in C++ using the fstream class for file I/O operations. The user is experiencing issues with writing data to a file, despite successfully opening and closing it. The specific line of code in question is _handle.open(fileName_, std::ios::in | std::ios::out | std::ios::app), which is intended for both input and output. The user seeks guidance on correctly writing data to the file, as attempts using _handle.put('s') and _handle << line << endl; have not yielded results.

PREREQUISITES
  • Understanding of C++ file I/O operations using fstream
  • Familiarity with C++ data types and macros, specifically const char
  • Knowledge of C++ class and method structures
  • Basic debugging techniques in C++
NEXT STEPS
  • Research C++ fstream modes and their implications on file operations
  • Learn about the correct usage of the std::ios::app flag in file writing
  • Explore C++ error handling techniques for file operations
  • Investigate alternative methods for writing to files, such as using std::ofstream
USEFUL FOR

C++ developers, software engineers working on file handling, and anyone interested in building custom database solutions in C++.

X89codered89X
Messages
149
Reaction score
2
Mod note: Instead of using HTML FONT and SIZE tags, just put [ code ] and [ /code ] tags around your code.[/color]
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
I think this would work.
Code:
_handle << line << endl;
 
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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
3K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
Replies
10
Views
2K