24 std::vector<uint8_t>& out_data,
const std::string& fileName)
34 size_t NN = fi.
Read(&out_data[0], N);
47 const std::vector<uint8_t>& vec,
const std::string& fileName)
52 if (!vec.empty()) of.
Write(&vec[0],
sizeof(vec[0]) * vec.size());
62 std::vector<std::string>& o,
const std::string& fileName)
65 std::ifstream f(fileName);
66 if (!f.is_open())
return false;
68 while (std::getline(f, s)) o.emplace_back(std::move(s));
77 std::ifstream t(fileName, ios::binary);
80 "file_get_contents(): Error opening for read file `%s`",
84 std::size_t
size = t.tellg();
85 std::string buffer(
size,
' ');
87 t.read(&buffer[0],
size);
92 const vector<float>& vec,
const string& fileName,
bool append,
bool byRows)
94 FILE* f =
os::fopen(fileName.c_str(), append ?
"at" :
"wt");
97 for (
float it : vec)
os::fprintf(f, byRows ?
"%e " :
"%e\n", it);
106 const vector<double>& vec,
const string& fileName,
bool append,
bool byRows)
108 FILE* f =
os::fopen(fileName.c_str(), append ?
"at" :
"wt");
109 if (!f)
return false;
111 for (
double it : vec)
os::fprintf(f, byRows ?
"%e " :
"%e\n", it);
120 const vector<int>& vec,
const string& fileName,
bool append,
bool byRows)
122 FILE* f =
os::fopen(fileName.c_str(), append ?
"at" :
"wt");
123 if (!f)
return false;
125 for (
int it : vec)
os::fprintf(f, byRows ?
"%i " :
"%i\n", it);
134 const vector<size_t>& vec,
const string& fileName,
bool append,
bool byRows)
136 FILE* f =
os::fopen(fileName.c_str(), append ?
"at" :
"wt");
137 if (!f)
return false;
139 for (
unsigned long it : vec)
140 os::fprintf(f, byRows ?
"%u " :
"%u\n", static_cast<unsigned int>(it));
149 std::vector<double>& vec,
const std::string& fileName,
bool byRows)
151 FILE* f =
os::fopen(fileName.c_str(),
"r");
152 if (!f)
return false;
158 size_t readed = fscanf(f, byRows ?
"%lf" :
"%lf\n", &number);
159 if ((!byRows) || (readed == 1)) vec.push_back(number);
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.
bool loadBinaryFile(std::vector< uint8_t > &out_data, const std::string &fileName)
Loads a entire file as a vector of bytes.
bool loadTextFile(std::vector< std::string > &o, const std::string &fileName)
Loads a text file as a vector of string lines.
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.
const_iterator end() const
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.
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.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
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.