C++17: Reading and Processing JSON Data for Chinese Poetry
#include
using namespace std;
int main() {
// Step 1: create vector of strings to store Chinese poetry
vector
// Step 2: read in x.json line by line and store each line as a string object into the vector
ifstream infile("x.json");
string line;
while (getline(infile, line)) {
poetry.push_back(line);
}
infile.close();
// Step 3: output the vector to D:\out.txt (and optionally, the screen)
ofstream outfile("D:\out.txt");
for (string s : poetry) {
outfile << s << endl;
cout << s << endl;
}
outfile.close();
// Step 4: read in x.json 1 character by character
infile.open("x.json");
char c;
string word = "";
while (infile.get(c)) {
if (c == '\n') {
poetry.push_back(word);
word = "";
}
else {
word += c;
}
}
infile.close();
return 0;
}
原文地址: https://www.cveoy.top/t/topic/nwCB 著作权归作者所有。请勿转载和采集!