Main MRPT website > C++ reference for MRPT 1.5.9
vector_loadsave.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
12 #include <mrpt/system/os.h>
16 
17 using namespace mrpt;
18 using namespace mrpt::utils;
19 using namespace mrpt::system;
20 using namespace std;
21 
22 bool mrpt::system::vectorToTextFile( const vector<float> &vec, const string &fileName, bool append, bool byRows )
23 {
24  FILE *f=os::fopen(fileName.c_str(), append ? "at" : "wt");
25  if (!f) return false;
26 
27  for (vector<float>::const_iterator it=vec.begin();it!=vec.end();++it)
28  os::fprintf(f,byRows ? "%e ":"%e\n",*it);
29 
30  if (byRows) os::fprintf(f,"\n");
31 
32  os::fclose(f);
33  return true; // All ok.
34 }
35 
36 bool mrpt::system::vectorToTextFile( const vector<double> &vec, const string &fileName, bool append, bool byRows )
37 {
38  FILE *f=os::fopen(fileName.c_str(),append ? "at" : "wt");
39  if (!f) return false;
40 
41  for (vector<double>::const_iterator it=vec.begin();it!=vec.end();++it)
42  os::fprintf(f,byRows ? "%e ":"%e\n",*it);
43 
44  if (byRows) os::fprintf(f,"\n");
45 
46  os::fclose(f);
47  return true; // All ok.
48 }
49 
50 bool mrpt::system::vectorToTextFile( const vector<int> &vec, const string &fileName, bool append, bool byRows )
51 {
52  FILE *f=os::fopen(fileName.c_str(),append ? "at" : "wt");
53  if (!f) return false;
54 
55  for (vector<int>::const_iterator it=vec.begin();it!=vec.end();++it)
56  os::fprintf(f,byRows ? "%i ":"%i\n",*it);
57 
58  if (byRows) os::fprintf(f,"\n");
59 
60  os::fclose(f);
61  return true; // All ok.
62 }
63 
64 bool mrpt::system::vectorToTextFile( const vector<size_t> &vec, const string &fileName, bool append, bool byRows )
65 {
66  FILE *f=os::fopen(fileName.c_str(),append ? "at" : "wt");
67  if (!f) return false;
68 
69  for (vector<size_t>::const_iterator it=vec.begin();it!=vec.end();++it)
70  os::fprintf(f,byRows ? "%u ":"%u\n",static_cast<unsigned int>(*it));
71 
72  if (byRows) os::fprintf(f,"\n");
73 
74  os::fclose(f);
75  return true; // All ok.
76 }
77 
78 bool mrpt::system::vectorFromTextFile( std::vector<double> &vec, const std::string &fileName, bool byRows )
79 {
80  FILE *f = os::fopen( fileName.c_str(), "r" );
81  if (!f) return false;
82 
83  double number = 0;
84 
85  while ( !feof(f) )
86  {
87  size_t readed = fscanf( f, byRows ? "%lf" : "%lf\n", &number );
88  if ( (!byRows) || (readed == 1) )
89  vec.push_back( number );
90  }
91 
92  return true;
93 }
94 
95 /*---------------------------------------------------------------
96  loadBinaryFile
97  ---------------------------------------------------------------*/
98 bool mrpt::system::loadBinaryFile( vector_byte &out_data, const std::string &fileName )
99 {
100  try
101  {
102  CFileInputStream fi(fileName);
103  size_t N = fi.getTotalBytesCount();
104 
105  out_data.resize(N);
106  if (N)
107  {
108  size_t NN = fi.ReadBuffer( &out_data[0], N);
109  return NN==N;
110  }
111  else return true;
112  }
113  catch(...) { return false; }
114 }
115 
116 /*---------------------------------------------------------------
117  vectorToBinaryFile
118  ---------------------------------------------------------------*/
119 bool mrpt::system::vectorToBinaryFile( const vector_byte &vec, const std::string &fileName )
120 {
121  try
122  {
123  mrpt::utils::CFileOutputStream of(fileName);
124  if (!vec.empty())
125  of.WriteBuffer( &vec[0], sizeof(vec[0])*vec.size() );
126  return true;
127  }
128  catch(...) { return false; }
129 }
size_t ReadBuffer(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer.
Definition: CStream.cpp:45
FILE BASE_IMPEXP * fopen(const char *fileName, const char *mode) MRPT_NO_THROWS
An OS-independent version of fopen.
Definition: os.cpp:255
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
Definition: zip.h:16
std::vector< uint8_t > vector_byte
Definition: types_simple.h:26
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:29
int BASE_IMPEXP void BASE_IMPEXP fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:272
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
Definition: CStream.cpp:67
int BASE_IMPEXP fprintf(FILE *fil, const char *format,...) MRPT_NO_THROWS MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:412
STL namespace.
bool BASE_IMPEXP vectorToBinaryFile(const vector_byte &vec, const std::string &fileName)
Saves a vector directly as a binary dump to a file:
const Scalar * const_iterator
Definition: eigen_plugins.h:24
This CStream derived class allow using a file as a write-only, binary stream.
GLsizei const GLchar ** string
Definition: glext.h:3919
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
bool BASE_IMPEXP vectorFromTextFile(std::vector< double > &vec, const std::string &fileName, const bool byRows=false)
Load a std::vector from a text file (compat.
This CStream derived class allow using a file as a read-only, binary stream.
uint64_t getTotalBytesCount() MRPT_OVERRIDE
Method for getting the total number of bytes in the buffer.
bool BASE_IMPEXP vectorToTextFile(const std::vector< float > &vec, const std::string &fileName, bool append=false, bool byRows=false)
A useful function for debugging, which saves a std::vector into a text file (compat.
bool BASE_IMPEXP loadBinaryFile(vector_byte &out_data, const std::string &fileName)
Loads a entire file as a vector of bytes.



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020