Last active
April 27, 2016 07:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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