NumCpp  2.5.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
fromfile.hpp
Go to the documentation of this file.
1 #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:
56  template<typename dtype>
57  NdArray<dtype> fromfile(const std::string& inFilename, const std::string& inSep = "")
58  {
60 
61  if (!filesystem::File(inFilename).exists())
62  {
63  THROW_INVALID_ARGUMENT_ERROR("input filename does not exist.\n\t" + inFilename);
64  }
65 
66  if (inSep.empty())
67  {
68  // read in as binary file
69  std::ifstream file(inFilename.c_str(), std::ios::in | std::ios::binary);
70  if (!file.is_open())
71  {
72  THROW_INVALID_ARGUMENT_ERROR("unable to open file\n\t" + inFilename);
73  }
74 
75  file.seekg(0, std::ifstream::end);
76  const uint32 fileSize = static_cast<uint32>(file.tellg());
77  file.seekg(0, std::ifstream::beg);
78 
79  std::vector<char> fileBuffer;
80  fileBuffer.reserve(fileSize);
81  file.read(fileBuffer.data(), fileSize);
82 
83  if (file.bad() || file.fail())
84  {
85  THROW_INVALID_ARGUMENT_ERROR("error occured while reading the file");
86  }
87 
88  file.close();
89 
90  NdArray<dtype> returnArray(reinterpret_cast<dtype*>(fileBuffer.data()), fileSize / sizeof(dtype));
91 
92  return returnArray;
93  }
94 
95  // read in as txt file
96  if (!(inSep == " " || inSep == "\t" || inSep == "\n"))
97  {
98  THROW_INVALID_ARGUMENT_ERROR("only [' ', '\\t', '\\n'] seperators are supported");
99  }
100 
101  std::vector<dtype> values;
102 
103  std::ifstream file(inFilename.c_str());
104  if (file.is_open())
105  {
106  uint32 lineNumber = 0;
107  while (!file.eof())
108  {
109  std::string line;
110  std::getline(file, line);
111 
112  std::istringstream iss(line);
113  try
114  {
115  dtype value;
116  while (iss >> value)
117  {
118  values.push_back(value);
119  }
120  }
121  catch (const std::invalid_argument& ia)
122  {
123  std::cout << "Warning: fromfile: line " << lineNumber << "\n" << ia.what() << std::endl;
124  }
125  catch (...)
126  {
127  std::cout << "Warning: fromfile: line " << lineNumber << std::endl;
128  }
129 
130  ++lineNumber;
131  }
132  file.close();
133  }
134  else
135  {
136  THROW_INVALID_ARGUMENT_ERROR("unable to open file\n\t" + inFilename);
137  }
138 
139  return NdArray<dtype>(values);
140 
141  }
142 } // 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, const std::string &inSep="")
Definition: fromfile.hpp:57
std::uint32_t uint32
Definition: Types.hpp:40