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-2018, 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 "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 (vector<float>::const_iterator it = vec.begin(); it != vec.end(); ++it)
26  os::fprintf(f, byRows ? "%e " : "%e\n", *it);
27 
28  if (byRows) os::fprintf(f, "\n");
29 
30  os::fclose(f);
31  return true; // All ok.
32 }
33 
35  const vector<double>& vec, const string& fileName, bool append, bool byRows)
36 {
37  FILE* f = os::fopen(fileName.c_str(), append ? "at" : "wt");
38  if (!f) return false;
39 
40  for (vector<double>::const_iterator it = vec.begin(); it != vec.end(); ++it)
41  os::fprintf(f, byRows ? "%e " : "%e\n", *it);
42 
43  if (byRows) os::fprintf(f, "\n");
44 
45  os::fclose(f);
46  return true; // All ok.
47 }
48 
50  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 
65  const vector<size_t>& vec, const string& fileName, bool append, bool byRows)
66 {
67  FILE* f = os::fopen(fileName.c_str(), append ? "at" : "wt");
68  if (!f) return false;
69 
70  for (vector<size_t>::const_iterator it = vec.begin(); it != vec.end(); ++it)
71  os::fprintf(f, byRows ? "%u " : "%u\n", static_cast<unsigned int>(*it));
72 
73  if (byRows) os::fprintf(f, "\n");
74 
75  os::fclose(f);
76  return true; // All ok.
77 }
78 
80  std::vector<double>& vec, const std::string& fileName, bool byRows)
81 {
82  FILE* f = os::fopen(fileName.c_str(), "r");
83  if (!f) return false;
84 
85  double number = 0;
86 
87  while (!feof(f))
88  {
89  size_t readed = fscanf(f, byRows ? "%lf" : "%lf\n", &number);
90  if ((!byRows) || (readed == 1)) vec.push_back(number);
91  }
92 
93  return true;
94 }
int void fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:273
STL namespace.
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:406
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:255
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.
const Scalar * const_iterator
Definition: eigen_plugins.h:27



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020