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...
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);
returntrue;
}
elsereturnfalse;
}
//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
unsignedint * 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 = newchar[4];
udata[0] = (unsignedchar)(data[i] >> 24);
udata[1] = (unsignedchar)(data[i] >> 16);
udata[2] = (unsignedchar)(data[i] >> 8);
udata[3] = (unsignedchar)(data[i]);
//Here it should write
DatabaseHelper::write.write(udata, 4);
}
//And here it should flush
DatabaseHelper::write.flush();
}
}
The system is running under a mipsel uprocessor, which is using a file system on my pc via NFS.
I think that maybe you need to use this-> instead of DatabaseHelper:: inside of the methods. I've never seen anyone use the class name explicitly like that inside a method, so if it does work like this-> then feel free to correct me...
Using this-> means I need to have a reference for the class. Since is a static field, I need to use the DatabaseHelper:: to use it's fields on the .cpp file.
//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);
and this:
1 2 3 4 5 6 7 8 9 10 11 12 13
for (int i = 0; i < size; ++i)
{
char * udata = newchar[4];
udata[0] = (unsignedchar)(data[i] >> 24);
udata[1] = (unsignedchar)(data[i] >> 16);
udata[2] = (unsignedchar)(data[i] >> 8);
udata[3] = (unsignedchar)(data[i]);
//Here it should write
DatabaseHelper::write.write(udata, 4);
}
//And here it should flush
DatabaseHelper::write.flush();
I know that, but I can surelly tell you that I don't use both at the same time. And also, I commented the code that opens the read stream but the problem remains...
This code was working perfectly, but when I changed the compiler...
I was using gcc 4.2, now I have to use a 3.2.3 gcc compiled for mipsel.
I'll try valgrind today, let's see what it has to say...
i m a noob in coding(see my post size).well i jus wanna ask wat this code is about as i havn't seen this kinda functions. r they inbuilt functions? n wat libraries do v need to include.
:)