contrib/mul/mbl/mbl_save_text_file.txx
Go to the documentation of this file.
00001 #ifndef mbl_save_text_file_txx_
00002 #define mbl_save_text_file_txx_
00003 //:
00004 // \file
00005 // \brief Functions to save objects to text file
00006 // \author dac
00007 
00008 #include "mbl_save_text_file.h"
00009 
00010 #include <mbl/mbl_exception.h>
00011 #include <vcl_fstream.h>
00012 
00013 //: Save vector to file with format  { n v1 v2 .. vn }
00014 template <class T>
00015 bool mbl_save_text_file(const vcl_vector<T >& v, const vcl_string& path)
00016 {
00017   vcl_ofstream ofs(path.c_str());
00018   if (!ofs)
00019   {
00020     mbl_exception_throw_os_error( path, "mbl_save_text_file: failed to save" );
00021     return false;
00022   }
00023 
00024   //ofs<<v.size()<<'\n';
00025   for (unsigned i=0;i<v.size();++i)
00026   {
00027     ofs<<v[i]<<'\n';
00028   }
00029   ofs.close();
00030   return true;
00031 }
00032 
00033 
00034 #undef MBL_SAVE_TEXT_FILE_INSTANTIATE
00035 #define MBL_SAVE_TEXT_FILE_INSTANTIATE(T ) \
00036 template bool mbl_save_text_file(const vcl_vector<T >& v, const vcl_string& path)
00037 
00038 #endif // mbl_save_text_file_txx_