MRPT  2.0.4
vector_loadsave.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "io-precomp.h" // Precompiled headers
11 
12 #include <mrpt/core/exceptions.h>
16 #include <mrpt/system/os.h>
17 
18 using namespace mrpt;
19 using namespace mrpt::io;
20 using namespace std;
21 namespace os = mrpt::system::os;
22 
24  std::vector<uint8_t>& out_data, const std::string& fileName)
25 {
26  try
27  {
28  CFileInputStream fi(fileName);
29  size_t N = fi.getTotalBytesCount();
30 
31  out_data.resize(N);
32  if (N)
33  {
34  size_t NN = fi.Read(&out_data[0], N);
35  return NN == N;
36  }
37  else
38  return true;
39  }
40  catch (...)
41  {
42  return false;
43  }
44 }
45 
47  const std::vector<uint8_t>& vec, const std::string& fileName)
48 {
49  try
50  {
51  mrpt::io::CFileOutputStream of(fileName);
52  if (!vec.empty()) of.Write(&vec[0], sizeof(vec[0]) * vec.size());
53  return true;
54  }
55  catch (...)
56  {
57  return false;
58  }
59 }
60 
62  std::vector<std::string>& o, const std::string& fileName)
63 {
64  o.clear();
65  std::ifstream f(fileName);
66  if (!f.is_open()) return false;
67  std::string s;
68  while (std::getline(f, s)) o.emplace_back(std::move(s));
69  return true;
70 }
71 
72 std::string mrpt::io::file_get_contents(const std::string& fileName)
73 {
74  // Credits: https://stackoverflow.com/a/2602258/1631514
75  // Note: Add "binary" to make sure the "tellg" file size matches the actual
76  // number of read bytes afterwards:
77  std::ifstream t(fileName, ios::binary);
78  if (!t.is_open())
80  "file_get_contents(): Error opening for read file `%s`",
81  fileName.c_str());
82 
83  t.seekg(0, std::ios::end);
84  std::size_t size = t.tellg();
85  std::string buffer(size, ' ');
86  t.seekg(0);
87  t.read(&buffer[0], size);
88  return buffer;
89 }
90 
92  const vector<float>& vec, const string& fileName, bool append, bool byRows)
93 {
94  FILE* f = os::fopen(fileName.c_str(), append ? "at" : "wt");
95  if (!f) return false;
96 
97  for (float it : vec) os::fprintf(f, byRows ? "%e " : "%e\n", it);
98 
99  if (byRows) os::fprintf(f, "\n");
100 
101  os::fclose(f);
102  return true; // All ok.
103 }
104 
106  const vector<double>& vec, const string& fileName, bool append, bool byRows)
107 {
108  FILE* f = os::fopen(fileName.c_str(), append ? "at" : "wt");
109  if (!f) return false;
110 
111  for (double it : vec) os::fprintf(f, byRows ? "%e " : "%e\n", it);
112 
113  if (byRows) os::fprintf(f, "\n");
114 
115  os::fclose(f);
116  return true; // All ok.
117 }
118 
120  const vector<int>& vec, const string& fileName, bool append, bool byRows)
121 {
122  FILE* f = os::fopen(fileName.c_str(), append ? "at" : "wt");
123  if (!f) return false;
124 
125  for (int it : vec) os::fprintf(f, byRows ? "%i " : "%i\n", it);
126 
127  if (byRows) os::fprintf(f, "\n");
128 
129  os::fclose(f);
130  return true; // All ok.
131 }
132 
134  const vector<size_t>& vec, const string& fileName, bool append, bool byRows)
135 {
136  FILE* f = os::fopen(fileName.c_str(), append ? "at" : "wt");
137  if (!f) return false;
138 
139  for (unsigned long it : vec)
140  os::fprintf(f, byRows ? "%u " : "%u\n", static_cast<unsigned int>(it));
141 
142  if (byRows) os::fprintf(f, "\n");
143 
144  os::fclose(f);
145  return true; // All ok.
146 }
147 
149  std::vector<double>& vec, const std::string& fileName, bool byRows)
150 {
151  FILE* f = os::fopen(fileName.c_str(), "r");
152  if (!f) return false;
153 
154  double number = 0;
155 
156  while (!feof(f))
157  {
158  size_t readed = fscanf(f, byRows ? "%lf" : "%lf\n", &number);
159  if ((!byRows) || (readed == 1)) vec.push_back(number);
160  }
161 
162  return true;
163 }
bool vectorToBinaryFile(const std::vector< uint8_t > &vec, const std::string &fileName)
Saves a vector directly as a binary dump to a file:
size_t size(const MATRIXLIKE &m, const int dim)
int void fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:286
STL namespace.
bool loadBinaryFile(std::vector< uint8_t > &out_data, const std::string &fileName)
Loads a entire file as a vector of bytes.
This CStream derived class allow using a file as a read-only, binary stream.
bool loadTextFile(std::vector< std::string > &o, const std::string &fileName)
Loads a text file as a vector of string lines.
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
This CStream derived class allow using a file as a write-only, binary stream.
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:419
const_iterator end() const
Definition: ts_hash_map.h:246
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
uint64_t getTotalBytesCount() const override
Returns the total amount of bytes in the stream.
std::string file_get_contents(const std::string &fileName)
Loads an entire text file and return its contents as a single std::string.
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
Definition: os.cpp:268
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
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 numeric std::vector as a plain-text file compatible wi...
bool vectorNumericFromTextFile(std::vector< double > &vec, const std::string &fileName, const bool byRows=false)
Load a numeric std::vector<double> from a text file (compat.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020