Main MRPT website > C++ reference for MRPT 1.9.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 
23  const vector<float>& vec, const string& fileName, bool append, bool byRows)
24 {
25  FILE* f = os::fopen(fileName.c_str(), append ? "at" : "wt");
26  if (!f) return false;
27 
28  for (vector<float>::const_iterator it = vec.begin(); it != vec.end(); ++it)
29  os::fprintf(f, byRows ? "%e " : "%e\n", *it);
30 
31  if (byRows) os::fprintf(f, "\n");
32 
33  os::fclose(f);
34  return true; // All ok.
35 }
36 
38  const vector<double>& vec, const string& fileName, bool append, bool byRows)
39 {
40  FILE* f = os::fopen(fileName.c_str(), append ? "at" : "wt");
41  if (!f) return false;
42 
43  for (vector<double>::const_iterator it = vec.begin(); it != vec.end(); ++it)
44  os::fprintf(f, byRows ? "%e " : "%e\n", *it);
45 
46  if (byRows) os::fprintf(f, "\n");
47 
48  os::fclose(f);
49  return true; // All ok.
50 }
51 
53  const vector<int>& vec, const string& fileName, bool append, bool byRows)
54 {
55  FILE* f = os::fopen(fileName.c_str(), append ? "at" : "wt");
56  if (!f) return false;
57 
58  for (vector<int>::const_iterator it = vec.begin(); it != vec.end(); ++it)
59  os::fprintf(f, byRows ? "%i " : "%i\n", *it);
60 
61  if (byRows) os::fprintf(f, "\n");
62 
63  os::fclose(f);
64  return true; // All ok.
65 }
66 
68  const vector<size_t>& vec, const string& fileName, bool append, bool byRows)
69 {
70  FILE* f = os::fopen(fileName.c_str(), append ? "at" : "wt");
71  if (!f) return false;
72 
73  for (vector<size_t>::const_iterator it = vec.begin(); it != vec.end(); ++it)
74  os::fprintf(f, byRows ? "%u " : "%u\n", static_cast<unsigned int>(*it));
75 
76  if (byRows) os::fprintf(f, "\n");
77 
78  os::fclose(f);
79  return true; // All ok.
80 }
81 
83  std::vector<double>& vec, const std::string& fileName, bool byRows)
84 {
85  FILE* f = os::fopen(fileName.c_str(), "r");
86  if (!f) return false;
87 
88  double number = 0;
89 
90  while (!feof(f))
91  {
92  size_t readed = fscanf(f, byRows ? "%lf" : "%lf\n", &number);
93  if ((!byRows) || (readed == 1)) vec.push_back(number);
94  }
95 
96  return true;
97 }
98 
99 /*---------------------------------------------------------------
100  loadBinaryFile
101  ---------------------------------------------------------------*/
103  vector_byte& out_data, const std::string& fileName)
104 {
105  try
106  {
107  CFileInputStream fi(fileName);
108  size_t N = fi.getTotalBytesCount();
109 
110  out_data.resize(N);
111  if (N)
112  {
113  size_t NN = fi.ReadBuffer(&out_data[0], N);
114  return NN == N;
115  }
116  else
117  return true;
118  }
119  catch (...)
120  {
121  return false;
122  }
123 }
124 
125 /*---------------------------------------------------------------
126  vectorToBinaryFile
127  ---------------------------------------------------------------*/
129  const vector_byte& vec, const std::string& fileName)
130 {
131  try
132  {
133  mrpt::utils::CFileOutputStream of(fileName);
134  if (!vec.empty()) of.WriteBuffer(&vec[0], sizeof(vec[0]) * vec.size());
135  return true;
136  }
137  catch (...)
138  {
139  return false;
140  }
141 }
size_t ReadBuffer(void *Buffer, size_t Count)
Reads a block of bytes from the stream into Buffer.
Definition: CStream.cpp:40
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
std::vector< uint8_t > vector_byte
Definition: types_simple.h:27
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:30
int void 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:64
STL namespace.
bool 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:27
This CStream derived class allow using a file as a write-only, binary stream.
GLsizei const GLchar ** string
Definition: glext.h:4101
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:405
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
bool vectorFromTextFile(std::vector< double > &vec, const std::string &fileName, const bool byRows=false)
Load a std::vector from a text file (compat.
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
Definition: os.cpp:254
This CStream derived class allow using a file as a read-only, binary stream.
bool 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.
uint64_t getTotalBytesCount() override
Method for getting the total number of bytes in the buffer.
bool 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.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019