开发者

C++ binary output problem

could someone tell me what is wrong with this code?

Out_file = new ofstream("ABC.dat",  std::ios::binary);

for (int i = 0; i < Elem->entries(); i++)
{
    co_ord_X = (*Elem)[i]->Getx(); co_ord_Y = (*Elem)[i]->Gety(); co_ord_Z = (*Elem)[i]->Getz();
    Intensity = (*Elem)[i]->GetInt(); 

    ofstream out_txt( "z2_out.txt",ios::app);
    out_txt<<co_ord_X<<"    "<<co_ord_Y<<"     "<<co_ord_Z <<"   "<<Intensity<<endl;
    out_txt.close();


    Out_file->write(reinterpret_cast<char*>(&co_ord_X), sizeof(double));
    Out_file->write(reinterpret_cast<char*>(&co_ord_Y), sizeof(double));
    Out_file->write(reinterpret_cast<char*>(&co_ord_Z), sizeof(double));
    Out_file->write(reinterpret_cast<char*>(&Intensity),    sizeof(double));
}

The variable Elem is a pointer to an Array. co_ord_X, co_ord_y, co_ord_y and Intensity are of type double. I am able to output the text file ("out_txt"), however I ha开发者_StackOverflow中文版ve problems writing the binary ("Out_file"). The strange thing is that under some circumstances it works (depending on the values of the variables co_ord_X, co_ord_y, co_ord_y and Intensity) and in other cases, it doesn't. Can someone please tell me what is wrong? Driving me crazy.

Regards, Charles.


You are dynamically allocating your ofstream object, which does not get closed after your work is done. On the second iteration of your loop you try to open a new file, file opening fails but you don't check for that either.

To solve this, use stack objects for file streams before your loop.

ofstream out_txt( "z2_out.txt");
ofstream Out_file("ABC.dat", std::ios::binary);
for (int i = 0; i < Elem->entries(); i++)
{
    co_ord_X = (*Elem)[i]->Getx(); co_ord_Y = (*Elem)[i]->Gety(); co_ord_Z = (*Elem)[i]->Getz();
    Intensity = (*Elem)[i]->GetInt(); 

    out_txt<<co_ord_X<<"    "<<co_ord_Y<<"     "<<co_ord_Z <<"   "<<Intensity<<endl;

    Out_file.write(reinterpret_cast<char*>(&co_ord_X), sizeof(double));
    Out_file.write(reinterpret_cast<char*>(&co_ord_Y), sizeof(double));
    Out_file.write(reinterpret_cast<char*>(&co_ord_Z), sizeof(double));
    Out_file.write(reinterpret_cast<char*>(&Intensity),    sizeof(double));
}


Both output streams are created each iteration. You are using ios::app for text stream that means the data will be added to end of file, so it works fine in this case, as expected. But the binary stream is created w/o this flag, so it truncates the file each time, and since you don't close the stream and create a new one for the same file there can happen anything.

To fix this issue just define the output stream before loop, and create it on stack or delete it after if you use it as a pointer.


Now I really feel like a moron. After reading in programming books to always delete pointers, I never do that, because I simply hadn't had any problems with them. Well, this one took me more than 1 week to ask for help and several hours to finally figure it out. I basically used EXACTLY the same code but deleted the "Out_file" pointer at the end, and had the output I was expecting.

For all the newbies like me, implement what you read! For all the guys who invested their time to help me with the problem, accept my sincere thanks and apologies.

Sincere regards, Deadie

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜