View Single Post
ofstream is not writing!!
Old
  (#1)
agenteg
Member
 
Status: Offline
Posts: 3
Join Date: Jan 2009
Thumbs down ofstream is not writing!! - 15-01-2009

Guys, i'm having trouble with my code... I just can't find any mistake, and this is freaking me out! It should open a file used as a Database, to write data in binary mode... But it does not write...

Please, someone help me:
Code:
 void DatabaseHelper::EnsureInitialized()
      {
        //Get File Path (on this case, ./DB/Database.fb
        std::string destination= Database_Location;
        destination.append(DatabaseConfigsTypeName);
        destination.append(DatabaseDefaultExtension);
        
        //Close both streams
        DatabaseHelper::read.close();
        DatabaseHelper::write.close();
      
        //If file does not exist
        if (!DatabaseHelper::DoesExist(destination))
          {
            //Create File
            DatabaseHelper::write.open(destination.c_str(), std::fstream::app);
            //Close stream
            DatabaseHelper::write.close();
          }
        
       //Now, open for use 
        DatabaseHelper::write.open(destination.c_str(), std::fstream::out
            | std::fstream::ate | std::fstream::binary);
        
       //Now, open for use      
        DatabaseHelper::read.open(destination.c_str(), std::fstream::in
            | std::fstream::ate| std::fstream::binary);
        
      }
    

    //This method just check if file exists
    bool DatabaseHelper::DoesExist(std::string path)
      {
        std::FILE * file;
        
        if (file = fopen(path.c_str(), "r"))
          {
            fclose(file);
            return true;
          }
        else
          return false;
      }
    
    //This method ensure that the file has data. The file in question is a Database Register Counter
    void DatabaseHelper::EnsureHasData()
      {
       //If database is not OK, throws exception
        if (!DatabaseHelper::read.is_open())
          if (!DatabaseHelper::read.good())
            {
              std::cerr
                  << "Read stream is not OK. Check stream before calling this method!"
                  << std::endl;
              throw "Stream is not OK. Check stream before calling this method!";
            }
        
          //Go to end
          DatabaseHelper::read.seekg(0, std::ios::end);
          //Gets the pointer
          int ptrPosition = DatabaseHelper::read.tellg();

        //Compare with the data that should be there. Always go there
        if (ptrPosition != sizeof(DatabaseConfigurations))
          {
            //Although it's empty, the stream is always OK
            if (!DatabaseHelper::write.is_open())
              if (!DatabaseHelper::write.good())
                {
                  std::cerr
                      << "Write stream is not OK. Check stream before calling this method!"
                      << std::endl;
                  throw "Stream is not OK. Check stream before calling this method!";
                }
            
            //Allocate a temporary counter
            DatabaseConfigurations configs = DatabaseConfigurations();
            
            //Get's the pointer
            unsigned int * data = *configs;
            //Get's size
            int size = configs.GetSize();
            //This never happened
            if (size == 0)
              {
                std::cerr
                    << "Error recording database configuration data. Size is 0"
                    << std::endl;
                throw "Error recording database configuration data. Size is 0";
              }
            //Also this
            if (data == 0)
              {
                std::cerr
                    << "Error recording database configuration data. Data points to 0"
                    << std::endl;
                throw "Error recording database configuration data. Data points to 0";
              }
            
            //Start writing on file. Altough a simple cout tells me that the data that is being written is OK, it does not write. What I do here is to get directly from the memory the values that should be written, so I can add fields on DatabaseConfigurations without the need to change anything else in the code.
            for (int i = 0; i < size; ++i)
              {
                char * udata = new char[4];
                udata[0] = (unsigned char)(data[i] >> 24);
                udata[1] = (unsigned char)(data[i] >> 16);
                udata[2] = (unsigned char)(data[i] >> 8);
                udata[3] = (unsigned char)(data[i]);

                //Here it should write
                DatabaseHelper::write.write(udata, 4);
              }
            //And here it should flush
            DatabaseHelper::write.flush();
          }
        
      }
I don't see any mistake, but it's not working! Hope you guys can help me!
  
Reply With Quote