MRPT  1.9.9
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-2019, 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 "system-precomp.h" // Precompiled headers
11 
12 #include <mrpt/system/os.h>
14 
15 using namespace mrpt;
16 using namespace mrpt::system;
17 using namespace std;
18 
20  const vector<float>& vec, const string& fileName, bool append, bool byRows)
21 {
22  FILE* f = os::fopen(fileName.c_str(), append ? "at" : "wt");
23  if (!f) return false;
24 
25  for (float it : vec) os::fprintf(f, byRows ? "%e " : "%e\n", it);
26 
27  if (byRows) os::fprintf(f, "\n");
28 
29  os::fclose(f);
30  return true; // All ok.
31 }
32 
34  const vector<double>& vec, const string& fileName, bool append, bool byRows)
35 {
36  FILE* f = os::fopen(fileName.c_str(), append ? "at" : "wt");
37  if (!f) return false;
38 
39  for (double it : vec) os::fprintf(f, byRows ? "%e " : "%e\n", it);
40 
41  if (byRows) os::fprintf(f, "\n");
42 
44  return true; // All ok.
45 }
46 
48  const vector<int>& vec, const string& fileName, bool append, bool byRows)
49 {
50  FILE* f = os::fopen(fileName.c_str(), append ? "at" : "wt");
51  if (!f) return false;
52 
53  for (int it : vec) os::fprintf(f, byRows ? "%i " : "%i\n", it);
54 
55  if (byRows) os::fprintf(f, "\n");
56 
57  os::fclose(f);
58  return true; // All ok.
59 }
60 
62  const vector<size_t>& vec, const string& fileName, bool append, bool byRows)
63 {
64  FILE* f = os::fopen(fileName.c_str(), append ? "at" : "wt");
65  if (!f) return false;
66 
67  for (unsigned long it : vec)
68  os::fprintf(f, byRows ? "%u " : "%u\n", static_cast<unsigned int>(it));
69 
70  if (byRows) os::fprintf(f, "\n");
71 
72  os::fclose(f);
73  return true; // All ok.
74 }
75 
77  std::vector<double>& vec, const std::string& fileName, bool byRows)
78 {
79  FILE* f = os::fopen(fileName.c_str(), "r");
80  if (!f) return false;
81 
82  double number = 0;
83 
84  while (!feof(f))
85  {
86  size_t readed = fscanf(f, byRows ? "%lf" : "%lf\n", &number);
87  if ((!byRows) || (readed == 1)) vec.push_back(number);
88  }
89 
90  return true;
91 }
int void fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:275
STL namespace.
GLsizei const GLchar ** string
Definition: glext.h:4116
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:410
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:257
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.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019