Skip to content
Snippets Groups Projects
Commit 7c5566a6 authored by Tom Fischer's avatar Tom Fischer
Browse files

[FileTools.h] Added readBinaryArray().

parent d436e740
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,10 @@ ...@@ -17,6 +17,10 @@
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <vector>
// ThirdParty/logog
#include "logog/include/logog.hpp"
namespace BaseLib namespace BaseLib
{ {
...@@ -66,6 +70,35 @@ T readBinaryValue(std::istream& in) ...@@ -66,6 +70,35 @@ T readBinaryValue(std::istream& in)
return v; return v;
} }
template <typename T>
std::vector<T> readBinaryArray(std::string const& filename, std::size_t const n)
{
std::ifstream in(filename.c_str());
if (!in) {
ERR("readBinaryArray(): Error while reading from file \"%s\".", filename.c_str());
ERR("Could not open file \"%s\" for input.", filename.c_str());
in.close();
return std::vector<T>();
}
std::vector<T> result;
result.reserve(n);
for (std::size_t p = 0; in && !in.eof() && p < n; ++p)
result.push_back(BaseLib::readBinaryValue<T>(in));
if (result.size() == n)
return result;
ERR("readBinaryArray(): Error while reading from file \"%s\".", filename.c_str());
ERR("Read different number of values. Expected %d, got %d.", n, result.size());
if (!in.eof())
ERR("EOF reached.\n");
return std::vector<T>();
}
/** /**
* \brief truncate a file * \brief truncate a file
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment