CSV:
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
// 保存到CSV
void saveToCSV(const std::vector<std::string>& expressions, const std::string& filename) {
std::ofstream file(filename.c_str());
if (!file.is_open()) {
std::cerr << "Unable to open file for writing: " << filename << std::endl;
return;
}
for (std::vector<std::string>::const_iterator it = expressions.begin(); it != expressions.end(); ++it) {
file << *it << std::endl;
}
file.close();
}
// 从CSV读取
std::vector<std::string> readFromCSV(const std::string& filename) {
std::vector<std::string> expressions;
std::ifstream file(filename.c_str());
if (!file.is_open()) {
std::cerr << "Unable to open file for reading: " << filename << std::endl;
return expressions;
}
std::string line;
while (std::getline(file, line)) {
expressions.push_back(line);
}
file.close();
return expressions;
}
int main() {
std::vector<std::string> expressions;
expressions.push_back("2 + 2=4");
expressions.push_back("3 * 4=12");
expressions.push_back("5 - 1=4");
saveToCSV(expressions, "expressions.csv");
std::vector<std::string> loadedExpressions = readFromCSV("expressions.csv");
for (std::vector<std::string>::const_iterator it = loadedExpressions.begin(); it != loadedExpressions.end(); ++it) {
std::cout << *it << std::endl;
}
return 0;
}
XML:
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
// 保存到XML
void saveToXML(const std::vector<std::string>& expressions, const std::string& filename) {
std::ofstream file(filename.c_str()); // 使用 c_str() 方法
if (!file) {
std::cerr << "Unable to open file for writing: " << filename << std::endl;
return;
}
file << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
file << "<expressions>" << std::endl;
for (size_t i = 0; i < expressions.size(); ++i) {
file << " <expression>" << expressions[i] << "</expression>" << std::endl;
}
file << "</expressions>" << std::endl;
file.close();
}
// 从XML读取(简化版,不使用第三方库)
std::vector<std::string> readFromXML(const std::string& filename) {
std::vector<std::string> expressions;
std::ifstream file(filename.c_str()); // 使用 c_str() 方法
if (!file) {
std::cerr << "Unable to open file for reading: " << filename << std::endl;
return expressions;
}
std::string line;
std::string currentExpression;
bool inExpression = false;
while (getline(file, line)) {
size_t startPos = line.find("<expression>");
if (startPos != std::string::npos) {
inExpression = true;
// Remove the <expression> tag from the line
line = line.substr(startPos + 12);
}
if (inExpression) {
// Check if the line contains the closing tag
size_t endPos = line.find("</expression>");
if (endPos != std::string::npos) {
// Extract the expression text and add it to the list
currentExpression = line.substr(0, endPos);
expressions.push_back(currentExpression);
// Reset for the next expression
currentExpression.clear();
inExpression = false;
} else {
// If no closing tag, append the whole line
currentExpression += line;
}
}
}
file.close();
return expressions;
}
int main() {
std::vector<std::string> expressions;
expressions.push_back("2 + 2=4");
expressions.push_back("3 * 4=12");
expressions.push_back("5 - 1=4");
saveToXML(expressions, "expressions.xml");
std::vector<std::string> loadedExpressions = readFromXML("expressions.xml");
for (size_t i = 0; i < loadedExpressions.size(); ++i) {
std::cout << loadedExpressions[i] << std::endl;
}
return 0;
}
JSON:
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
// 保存到JSON
void saveToJSON(const std::vector<std::string>& expressions, const std::string& filename) {
std::ofstream file(filename.c_str()); // 使用 c_str() 方法
if (!file) {
std::cerr << "Unable to open file for writing: " << filename << std::endl;
return;
}
file << " " << std::endl;
for (size_t i = 0; i < expressions.size(); ++i) {
file << " \"" << expressions[i] << "\"";
if (i < expressions.size() - 1) {
file << ",";
}
file << std::endl;
}
file << " " << std::endl;
file.close();
}
// 从JSON读取(简化版,不使用第三方库)
std::vector<std::string> readFromJSON(const std::string& filename) {
std::vector<std::string> expressions;
std::ifstream file(filename.c_str()); // 使用 c_str() 方法
if (!file) {
std::cerr << "Unable to open file for reading: " << filename << std::endl;
return expressions;
}
std::string currentExpression;
char ch;
bool inQuotes = false;
while (file.get(ch)) {
if (ch == '"') {
inQuotes = !inQuotes;
} else if (ch == ',' && !inQuotes) {
// Ignore commas outside quotes
continue;
} else if (isspace(ch) && !inQuotes) {
// Ignore whitespace outside quotes
if (!currentExpression.empty()) {
expressions.push_back(currentExpression);
currentExpression.clear();
}
continue;
} else {
currentExpression += ch;
}
}
if (!currentExpression.empty()) {
expressions.push_back(currentExpression);
}
file.close();
return expressions;
}
int main() {
std::vector<std::string> expressions;
expressions.push_back("2 + 2=4");
expressions.push_back("3 * 4=12");
expressions.push_back("5 - 1=4");
saveToJSON(expressions, "expressions.json");
std::vector<std::string> loadedExpressions = readFromJSON("expressions.json");
for (size_t i = 0; i < loadedExpressions.size(); ++i) {
std::cout << loadedExpressions[i] << std::endl;
}
return 0;
}
标签:std,构造,vector,file,软件,expressions,include,line From: https://www.cnblogs.com/yuanxinglan/p/18487598