[Insight-users] why can not write observation to file?

Luis Ibanez luis.ibanez at kitware.com
Mon May 23 18:10:54 EDT 2005


Hi Yu,

It seems that you forgot to close the file.

Note that instead of the old fprintf approach,
it is much easier now to use ofstreams.


Like


         #include <fstream>


         std::ofstream myFile;
         myFile.open("filename.txt");
         myFile << "I like fstreams" << std::endl;
         myFile.close();



   Regards,


      Luis



----------------------
Yu Qi wrote:

> Hi,
> I want to write the result of observer to a file called Observer_Result.txt
> and my code is as follows, but why there is onthing in the file?
>  
>  
>  void Execute(const itk::Object * object, const itk::EventObject & event)
>     {
>       OptimizerPointer optimizer =
>         dynamic_cast< OptimizerPointer >( object );
>       if( typeid( event ) != typeid( itk::IterationEvent ) )
>         {
>         return;
>         }
>       std::cout << optimizer->GetCurrentIteration() << "   ";
>       std::cout << optimizer->GetValue() << "   ";
>       std::cout << optimizer->GetCurrentPosition() << std::endl;
>     /////////////////////////////////write to file
>     FILE *stream1;
>    char fileName1[]= "Observer_Result.txt";
>     if( (stream1 = fopen( fileName1, "a+t" )) != NULL )
>     { 
>     fprintf( stream1, "%d\t", optimizer->GetCurrentIteration());
>     fprintf( stream1, "%e\t",optimizer->GetValue());
>     OptimizerType::ParametersType observerParameters =
>                     optimizer->GetCurrentPosition();
>    
>     fprintf( stream1, "%e\t",  observerParameters[0]);
>     fprintf( stream1, "%e\t",  observerParameters[1]);
>     fprintf( stream1, "%e\t",  observerParameters[2]);
>     fprintf( stream1, "%e\t",  observerParameters[3]);
>     fprintf( stream1, "%e\t",  observerParameters[4]);
>     fprintf( stream1, "%e\t\n",  observerParameters[5]);
>   }
>  
>     }
> 
> 
> *Best Regards!*
> ** 
> *Yu Qi*
>  
>  
> 
> 
> ------------------------------------------------------------------------
> *Do You Yahoo!?*
> 150万曲MP3疯狂搜,带您闯入音乐殿堂 <http://music.yisou.com>
> 美女明星应有尽有,搜遍美图、艳图和酷图 <http://image.yisou.com>
> 1G就是1000兆,雅虎电邮自助扩容! 
> <http://cn.rd.yahoo.com/mail_cn/tag/1g/*http://cn.mail.yahoo.com/event/mail_1g/> 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users





More information about the Insight-users mailing list