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