.:: Bots United ::.  
filebase forums discord server github wiki web
cubebot epodbot fritzbot gravebot grogbot hpbbot ivpbot jkbotti joebot
meanmod podbotmm racc rcbot realbot sandbot shrikebot soulfathermaps yapb

Go Back   .:: Bots United ::. > Developer's Farm > General Programming
General Programming Help others and get yourself helped here!

Reply
 
Thread Tools
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
Re: ofstream is not writing!!
Old
  (#2)
Cheeseh
[rcbot]
 
Cheeseh's Avatar
 
Status: Offline
Posts: 361
Join Date: Dec 2003
Location: China
Default Re: ofstream is not writing!! - 16-01-2009

I dont see any easy mistakes there but that code may not be what is causing the problem. Are you sure you are closing the files properly after you have written everything to the file okay??


Ps i cant see all of the code because of my browser
  
Reply With Quote
Re: ofstream is not writing!!
Old
  (#3)
DrEvil
Member
 
DrEvil's Avatar
 
Status: Offline
Posts: 142
Join Date: Jan 2004
Location: Los Angeles, CA
Default Re: ofstream is not writing!! - 17-01-2009

Do your strings already have the slashes, dots and such in them? You are appending them without adding them if so.

Try std::fstream::app|std::fstream::out. I've never tried just app


Omni-bot AI framework
http://www.omni-bot.com

Foxbot - for Team Fortress Classic
http://www.foxbot.net


  
Reply With Quote
Re: ofstream is not writing!!
Old
  (#4)
agenteg
Member
 
Status: Offline
Posts: 3
Join Date: Jan 2009
Thumbs down Re: ofstream is not writing!! - 19-01-2009

I changed my code here as DrEvil said:

Code:
DatabaseHelper::write.open(destination.c_str(), std::fstream::app | std::fstream::out);
and here to try to find out why it's not working:
Code:
   if (!DatabaseHelper::write)
                  std::cout << "Stream Error:  " <<  (int)DatabaseHelper::write.rdstate() << std::endl;
using out flag did not help, and the return from rdstate() is always 4, that means failbit.

closing the stream after writing as Cheseeh said did not help too.

From ios_base.h

Quote:
Failbit: Indicates that an input operation failed to read the expected
characters, or that an output operation failed to generate the
desired characters.
No success yet. I'll keep trying.

Thank you very much guys... Any help will be very apreciated!
  
Reply With Quote
Re: ofstream is not writing!!
Old
  (#5)
agenteg
Member
 
Status: Offline
Posts: 3
Join Date: Jan 2009
Default Re: ofstream is not writing!! - 22-01-2009

Problem solved(?)...

If I use a
Code:
<<std::endl;
on the end of the writing, it flushes and write to file.

I'ts not a solution, but it's helping...
  
Reply With Quote
Re: ofstream is not writing!!
Old
  (#6)
mirv
Super Moderator
 
mirv's Avatar
 
Status: Offline
Posts: 152
Join Date: Apr 2004
Default Re: ofstream is not writing!! - 24-01-2009

Just for the sake of "it shouldn't do anything but try it anyway" use ostream or iostream instead of ofstream.


mirv the Silly Fish.
  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com