NumCpp  2.8.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
fromfile.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <fstream>
31 #include <memory>
32 #include <sstream>
33 #include <string>
34 
38 #include "NumCpp/Core/Types.hpp"
39 #include "NumCpp/NdArray.hpp"
40 
41 namespace nc
42 {
43  //============================================================================
44  // Method Description:
52  template<typename dtype>
53  NdArray<dtype> fromfile(const std::string& inFilename)
54  {
55  if (!filesystem::File(inFilename).exists())
56  {
57  THROW_INVALID_ARGUMENT_ERROR("input filename does not exist.\n\t" + inFilename);
58  }
59 
60  // read in as binary file
61  std::ifstream file(inFilename.c_str(), std::ios::in | std::ios::binary);
62  if (!file.is_open())
63  {
64  THROW_INVALID_ARGUMENT_ERROR("unable to open file\n\t" + inFilename);
65  }
66 
67  file.seekg(0, std::ifstream::end);
68  const uint32 fileSize = static_cast<uint32>(file.tellg());
69  file.seekg(0, std::ifstream::beg);
70 
71  std::vector<char> fileBuffer;
72  fileBuffer.reserve(fileSize);
73  file.read(fileBuffer.data(), fileSize);
74 
75  if (file.bad() || file.fail())
76  {
77  THROW_INVALID_ARGUMENT_ERROR("error occured while reading the file");
78  }
79 
80  file.close();
81 
82  NdArray<dtype> returnArray(reinterpret_cast<dtype*>(fileBuffer.data()), fileSize / sizeof(dtype));
83 
84  return returnArray;
85  }
86 
87  //============================================================================
88  // Method Description:
97  template<typename dtype>
98  NdArray<dtype> fromfile(const std::string& inFilename, const char inSep)
99  {
101 
102  std::ifstream file(inFilename.c_str());
103  if (!file.is_open())
104  {
105  THROW_INVALID_ARGUMENT_ERROR("unable to open file\n\t" + inFilename);
106  }
107 
108  std::vector<dtype> values;
109  uint32 lineNumber = 0;
110  while (!file.eof())
111  {
112  std::string line;
113  std::getline(file, line, inSep);
114 
115  std::istringstream iss(line);
116  try
117  {
118  dtype value;
119  while (iss >> value)
120  {
121  values.push_back(value);
122  }
123  }
124  catch (const std::invalid_argument& ia)
125  {
126  std::cout << "Warning: fromfile: line " << lineNumber << '\n' << ia.what() << std::endl;
127  }
128  catch (...)
129  {
130  std::cout << "Warning: fromfile: line " << lineNumber << std::endl;
131  }
132 
133  ++lineNumber;
134  }
135  file.close();
136 
137  return NdArray<dtype>(values);
138  }
139 } // namespace nc
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:36
#define STATIC_ASSERT_ARITHMETIC(dtype)
Definition: StaticAsserts.hpp:37
Holds 1D and 2D arrays, the main work horse of the NumCpp library.
Definition: NdArrayCore.hpp:72
Provides simple filesystem functions.
Definition: Filesystem.hpp:40
Definition: Coordinate.hpp:45
NdArray< dtype > fromfile(const std::string &inFilename)
Definition: fromfile.hpp:53
std::uint32_t uint32
Definition: Types.hpp:40