I’ve been working on a small programm that put all the images I need for my 3D engine into a single file, but for unknow reasons when I try to use fstream to write into a file, it doesn’t return any error, but still doesn’t write anything.
for exemple, I have a simple function that initialize a new file :
void initPAK(fstream& pakfile, image firstImg)
{
PAKheader head;
head.sign[0] = 'P';
head.sign[1] = 'A';
head.sign[2] = 'K';
head.nbdata = 1;
head.index.push_back(sizeof(head.sign)+sizeof(head.nbdata)+sizeof(uint32_t));
if(pakfile.is_open())
{
pakfile.write(head.sign, sizeof(head.sign));
pakfile.write((char*)&head.nbdata, sizeof(head.nbdata));
for(uint32_t n=0; n<head.index.size(); n++)
{
pakfile.write((char*)&head.index[n], sizeof(head.index[n]));
}
pakfile.write((char*)&firstImg.width, sizeof(firstImg.width));
pakfile.write((char*)&firstImg.height, sizeof(firstImg.height));
pakfile.write((char*)&firstImg.channels, sizeof(firstImg.channels));
for(uint32_t n=0; n<firstImg.data.size(); n++)
{
pakfile.write((char*)&firstImg.data[n], sizeof(firstImg.data[n]));
}
}
else
{
cerr << "unable to open" << endl;
}
}
And I use it like that :
fstream fileop;
fileop.open("bin_file", fstream::in | fstream::out | fstream::trunc | fstream::binary);
unsigned char zdata[] = {
255, 0, 0,
0, 255, 0,
0, 0, 255,
};
image zimg;
zimg.width = 3;
zimg.height = 1;
zimg.channels = 3;
for(int i=0; i < 9; i++)
{
zimg.data.push_back(zdata[i]);
}
initPAK(fileop, zimg);
fileop.close();
But the file “bin_file” his never write nor created. I saw in an other stackoverflow’s question that I should use flush() but I didn’t work either. The strangest thing his that this function use to work, until I replace ofstream to fstream I believe.
What am I doing wrong ?
Well, I’m a bit stupid. This function does work, my program was just so messy that the function was never called …
Thank anyway for your remarks who help me cleanup my code a bit.
Lesson learned : Never rush to code, even more if you should sleep.