Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

C++ Database Components: LINKED FROM PHYSICS FORUMS

By: x89codered89x on Oct 18th, 2012  |  syntax: C++  |  size: 3.52 KB  |  hits: 89  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /* LINKED FROM CPLUSPLUS.COM.....
  2.  
  3. 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?
  4.  
  5. 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.
  6.  
  7. I'll post the relevant code in this pastebin: http://pastebin.com/06ejZdKd
  8.  
  9.  
  10. 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.
  11.  
  12. Let me know what you think, and thanks in advance.
  13.  
  14.  
  15. */
  16.  
  17.  
  18.  
  19. //////////MAIN.CPP
  20.  
  21. #include "handler.h"
  22.  
  23. int main(){
  24.         Handler db;
  25.         db.addText("writeline");
  26.  
  27.         return 0;
  28. };
  29.  
  30.  
  31.  
  32.  
  33. /////////HANDLER.CPP
  34. //..
  35. //...
  36. class Handler{
  37.         public:
  38.         //Constructor(s)
  39.         Handler(cch* fileName_);
  40.         ~Handler(void);
  41.        
  42.         //Word Manipulation
  43.         bool addText(cch* line_);
  44. //..
  45. //..
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. //HANDLER.CXX
  53. //...
  54. ///...
  55. Handler::Handler(cch* fileName_ = NULL){       
  56.         if (fileName_ == NULL){
  57.                 fileName_ = "default.dat";
  58.         };
  59.         int fileNameID_ = UsefulFunctions<std::string>::findInstanceID(_fileNames,fileName_);
  60.  
  61.         if (fileNameID_ == -1/*File not loaded yet during this runtime*/){
  62.                 //register with lsit of loaded db's from this runtime
  63.                 fileNameID_=_fileNames.size();
  64.                 _fileNames.push_back(fileName_);
  65.  
  66.                 //create db if hadn't been created
  67.                 _handle.open(fileName_, std::ios::out | std::ios::app);
  68.                 _handle.close();
  69.  
  70.                 //check file length of the file
  71.                 _handle.open(fileName_,  std::ios::in | std::ios::out | std::ios::ate);
  72.                 this->getFileLength();
  73.                 if (_fileLength <= 0){
  74.                         _handle.write("fileName:\n",10);
  75.                         this->getFileLength();
  76.                 };
  77.  
  78.                 _handle.close();
  79.                 //this->setReadPosition(0);            
  80.                 //std::cout << "read->Position(): " << this->readPosition() << std::endl;              
  81.                 //this->writeLine("Write Line Test");
  82.                 //this->writeLine((stri("fileName:\n")+fileName_).c_str());            
  83.                
  84.                 //check file length
  85.                 _handle.open(fileName_, std::ios::in | std::ios::out | std::ios::app);
  86.                 this->setReadPosition(0);
  87.                 this->getFileLength();
  88.                 this->setWritePosition(0);
  89.  
  90.         };
  91. };
  92.  
  93. Handler::~Handler(void) {
  94.         if (_handle.is_open()){
  95.                 _handle.flush();
  96.                 _handle.close();
  97.         };
  98. };
  99.  
  100. bool Handler::addText(cch* line_){
  101.         if (_handle.is_open()){
  102.                 std::cout << "File Open!" << std::endl;
  103.                 _handle.put('s');
  104.         } else {
  105.                 std::cout << "File Not Open!" << std::endl;
  106.         };
  107.  return true;
  108. };
  109.  
  110.  
  111. //...
  112. //...
  113.  
  114. /*CONSOLE OUTPUT:
  115.  
  116. 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
  117.  
  118. E:\Dropbox\Projects\CandC++\mains\Game\v10c\Graphics Engine\database4\simpledb>e.exe
  119. File Open!
  120.  
  121. E:\Dropbox\Projects\CandC++\mains\Game\v10c\Graphics Engine\database4\simpledb>pause
  122. Press any key to continue . . .
  123.  
  124. */