Skip to content

Instantly share code, notes, and snippets.

@tenomoto
Last active April 27, 2016 07:28
Show Gist options
  • Save tenomoto/75a8fabb09f010b30d65d4e9a2bcaa8f to your computer and use it in GitHub Desktop.
Save tenomoto/75a8fabb09f010b30d65d4e9a2bcaa8f to your computer and use it in GitHub Desktop.
Read the whole content of a text file and return C++ std::vector
#include <iostream>
#include <fstream>
#include <vector>
template <typename T>
std::vector<T> read_ascii(std::string fname, bool verbose = false)
{
std::vector<T> data;
std::ifstream ifs (fname);
T buf;
if (ifs.is_open())
{
while (ifs >> buf)
{
data.push_back(buf);
}
}
if (verbose)
{
for (int j = 0; j < data.size(); j++)
{
std::cout << "data(" << j << ") = " << data[j] << std::endl;
}
}
ifs.close();
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment