/* LINKED FROM CPLUSPLUS.COM.....
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, anything very easily and edit it 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
Most of the code in the Handler constructor is superfluous at this point. I tend to code things I think I will need later and less on the problem at hand. Thus I have almost 1k lines and no working, functional database at all.
Let me know what you think, and thanks in advance.
*/
//////////MAIN.CPP
#include "handler.h"
int main(){
Handler db;
db.addText("writeline");
return 0;
};
/////////HANDLER.CPP
//..
//...
class Handler{
public:
//Constructor(s)
Handler(cch* fileName_);
~Handler(void);
//Word Manipulation
bool addText(cch* line_);
//..
//..
//HANDLER.CXX
//...
///...
Handler::Handler(cch* fileName_ = NULL){
if (fileName_ == NULL){
fileName_ = "default.dat";
};
int fileNameID_ = UsefulFunctions<std::string>::findInstanceID(_fileNames,fileName_);
if (fileNameID_ == -1/*File not loaded yet during this runtime*/){
//register with lsit of loaded db's from this runtime
fileNameID_=_fileNames.size();
_fileNames.push_back(fileName_);
//create db if hadn't been created
_handle.open(fileName_, std::ios::out | std::ios::app);
_handle.close();
//check file length of the file
_handle.open(fileName_, std::ios::in | std::ios::out | std::ios::ate);
this->getFileLength();
if (_fileLength <= 0){
_handle.write("fileName:\n",10);
this->getFileLength();
};
_handle.close();
//this->setReadPosition(0);
//std::cout << "read->Position(): " << this->readPosition() << std::endl;
//this->writeLine("Write Line Test");
//this->writeLine((stri("fileName:\n")+fileName_).c_str());
//check file length
_handle.open(fileName_, std::ios::in | std::ios::out | std::ios::app);
this->setReadPosition(0);
this->getFileLength();
this->setWritePosition(0);
};
};
Handler::~Handler(void) {
if (_handle.is_open()){
_handle.flush();
_handle.close();
};
};
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;
};
//...
//...
/*CONSOLE OUTPUT:
E:\Dropbox\Projects\CandC++\mains\Game\v10c\Graphics Engine\database4\simpledb>g++ main.cpp -o e.exe -lsfml-graphics -lsfml-window -lsfml-audio -lsfml-system -lopengl32 -lGLU32
E:\Dropbox\Projects\CandC++\mains\Game\v10c\Graphics Engine\database4\simpledb>e.exe
File Open!
E:\Dropbox\Projects\CandC++\mains\Game\v10c\Graphics Engine\database4\simpledb>pause
Press any key to continue . . .
*/