Search code examples
c++ioitk

Random error to write into a file in CPP


I have got two programs written in c++ (to simplify it is A and B). A and B use ITK and A uses also Boost. I use the following procedure:

A calls B. B creates a file and writes into. Then A reads it.

Sometimes, B can't create the file and sometimes A can't read. But not always. I have tried to add a pre-step: A creates the file. But it doesnt change anything.

Have you ever heard about such behaviour? Do you have any idea?

A uses basic std::ifstream and B uses:

itk::TransformFileWriter::Pointer affineWriter;
  affineWriter = itk::TransformFileWriter::New();
  if( dofoutName != NULL )
  {
          affineWriter->SetFileName( dofoutName );
          affineWriter->SetInput( finalTransform   );
          affineWriter->Update();
  }

Solution

  • Maybe it is ifstream blocking it (see https://stackoverflow.com/a/750914/1136458 ). Check if the destructor of ifstream already called when you call B, or try not to use ifstream altogether.

    If it's not enough and you are on Windows, you can debug your application, stop it and check on ProcessExplorer which handle is locking your file