Skip to content

Instantly share code, notes, and snippets.

@hunandy14
Last active May 22, 2021 16:37
Show Gist options
  • Save hunandy14/425a64ab355affe215609a511db76a51 to your computer and use it in GitHub Desktop.
Save hunandy14/425a64ab355affe215609a511db76a51 to your computer and use it in GitHub Desktop.
C/C++ 依照特定格式 讀取檔案並 切割字串
/*
2021-05-19
詳細的說明可以參考我的部落格
https://charlottehong.blogspot.com/2021/05/cc.html
*/
#pragma
#include <vector>
#include <fstream>
#include <string>
#include <string_view>
using namespace std;
class OneLine {
private:
using _type = vector<string_view>;
public:
OneLine() {}
OneLine(_type oneline) {
data = oneline;
}
OneLine(const string_view strv, string_view delims = " ") {
splitSV(strv, delims);
}
operator _type() const {
return data;
}
operator vector<string>() const {
vector<string> str(data.size());
for (size_t i = 0; i < data.size(); i++)
str[i] = data[i];
return str;
}
string getStringIdx(size_t idx) const {
string s = "";
if (idx < data.size()) {
s = string(data[idx]);
} else {
throw runtime_error("range Error.");
}
return s;
}
long getIntIdx(size_t idx) const {
return stoull(getStringIdx(idx));
}
size_t size() {
return data.size();
}
_type splitSV(string_view strv, string_view delims = " ") {
_type output;
for (size_t first = 0; first < strv.size();) {
const auto second = strv.find_first_of(delims, first);
if (first != second)
output.emplace_back(strv.substr(first, second - first));
if (second == string_view::npos)
break;
first = second + 1;
}
if (output.size() == 0)
output.emplace_back("");
data = std::move(output);
return data;
}
std::istream& getline(string_view delims = " ") {
std::istream& is = std::getline(fs, str);
if (is) {
splitSV(str, delims);
}
return is;
}
void openFile(const string file_name) {
if (name != file_name) {
name = file_name;
fs.close();
fs.open(file_name, ios::in);
if (!fs.is_open())
throw runtime_error("Reading error.");
}
}
private:
_type data;
string str;
private:
string name;
fstream fs;
};
std::ostream& operator<<(std::ostream& out, vector<string_view>& v) {
cout << "[";
for (size_t i = 0; i < v.size(); i++) {
out << v[i];
if (i < v.size() - 1) { cout << ", "; }
}
cout << "]";
return out;
}
std::ostream& operator<<(std::ostream& out, OneLine& v) {
return out << (vector<string_view>&)v;
}
//=============================================================================
// 逐行讀取
vector<string> ReadFile_line(const string file_name) {
vector<string> v;
fstream fs(file_name, ios::in);
if (!fs.is_open())
throw runtime_error("Reading error.");
for (string str; getline(fs, str);) {
//cout << str << endl;
v.emplace_back(str);
} fs.seekg(0, fs.beg);
fs.close();
return v;
}
// 切割檔案
void rf_test1() {
auto&& v1 = ReadFile_line("in.txt");
vector<OneLine> v2;
for (auto&& i : v1) {
v2.emplace_back(OneLine(i, " "));
}
for (auto&& i : v2) {
cout << i << endl;
}
}
// 切割檔案 (懶加載)
void rf_test2() {
OneLine line;
line.openFile("a.txt");
while (line.getline()) {
vector<string_view>&& tokenList = line;
cout << tokenList << endl;
}
}
// 切割字串
void rf_test3() {
string str = "123 | 321";
OneLine line(str, " | ");
cout << line << endl;
vector<string_view>&& tokenList = line;
for (size_t i = 0; i < tokenList.size(); i++)
cout << tokenList[i] << endl;
}
// 返回字串
void rf_test_getIdx() {
string str = "123 | 321";
OneLine line(str, " | ");
cout << line.getStringIdx(2) << endl;
}
void rf_test() {
//rf_test1();
//rf_test2();
//rf_test3();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment