0

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 have 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.

1
  • What do you mean by 'doesn't work'? Commented Mar 6, 2011 at 13:39

3 Answers 3

1

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));
}
Sign up to request clarification or add additional context in comments.

8 Comments

Hi, well it still doesn't work. I get output with the text but not with the binary.
I put the declaration before the loop now. Opening the file each iteration would clear file contents, unless you use std::ios::app.
It is crazy, still wouldn't work. As usual, the txt part still works well.
@Deadie: Do you need to add anything other than the contents of Elem to ABC.dat? Also any clarification on what you expect and what you get can be helpful.
No, all I need are the contents of Elem. The portion of the code above is in a function that I named write(), which gets called at the end of a bigger function. This function is rightly called at the end, hence my having the expected output of text-file. The only problem is the binary portion. I have also placed the command: ofstream Out_file("ABC.dat", std::ios::binary) inside the function, instead of the constructor where I had it before, yet I get no output.
|
0

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.

1 Comment

Hi, well it still doesn't work. I get output with the text but not with the binary. More details in the discussion above.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.