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