#include <iostream> #include <fstream> #include <string> #include <map> #include <SimpleIni.h> #include <sstream> using namespace std; int main() { map<string, string> config; CSimpleIniA ini; ini.LoadFile("config.ini"); string filename = ini.GetValue("Section1", "filename", ""); config["filename"] = filename; string txt = ini.GetValue("Section1", "txt", ""); stringstream txt_stream(txt); string line; while (getline(txt_stream, line)) { config["txt"] += line + "\n"; } ofstream file(config["filename"]); if (file.is_open()) { file << config["txt"]; file.close(); cout << "文件已成功生成!" << endl; } else { cout << "无法创建文件。" << endl; } return 0; } 将这段代码升级成支持中文文本

时间: 2023-06-18 22:04:10 浏览: 62
#include <iostream> #include <fstream> #include <string> #include <map> #include <SimpleIni.h> #include <sstream> #include <locale> // 处理中文文本需要加入的头文件 #include <codecvt> // 处理中文文本需要加入的头文件 using namespace std; int main() { map<string, string> config; CSimpleIniA ini; ini.LoadFile("config.ini"); string filename = ini.GetValue("Section1", "filename", ""); config["filename"] = filename; string txt = ini.GetValue("Section1", "txt", ""); stringstream txt_stream(txt); wstring_convert<codecvt_utf8<wchar_t>> converter; // 创建一个 UTF-8 到本地宽字符编码转换器 wstring line; // 用宽字符类型存储读取的文本 while (getline(txt_stream, line)) { config["txt"] += converter.to_bytes(line) + "\n"; // 将宽字符转换为 UTF-8 编码 } wofstream file(config["filename"]); // 打开一个宽字符类型的输出流 if (file.is_open()) { file << converter.from_bytes(config["txt"]); // 将 UTF-8 编码转换为宽字符 file.close(); cout << "文件已成功生成!" << endl; } else { cout << "无法创建文件。" << endl; } return 0; }

相关推荐

优化改进以下代码#include <iostream> #include <fstream> #include <string> #include <vector> #include <algorithm> #include <iterator> using namespace std; typedef istream_iterator<string> string_input; void welcome() { cout << "******************* 变位词查找系统*********************\n" << "在词典中找出给定的字符串的所有变位词" << endl; } void readDict(vector<string> & dictionary) { cout << "首先,请输入词典的文件名称:" << endl; string dictionary_name; cin >> dictionary_name; ifstream ifs(dictionary_name.c_str()); if (!ifs.is_open()) { cerr << "异常:文件"<< dictionary_name << "没有找到 " << endl; exit(1); } cout << "词典读入中 ..." << flush; copy(string_input(ifs), string_input(), back_inserter(dictionary)); sort(dictionary.begin(),dictionary.end()); cout << "词典包含有 " << dictionary.size() << " 个单词\n\n"; ifs.close(); } void analyseAnagram(const vector<string> & dictionary) { cout << "请输入单词(或任意字母序列)" << endl; for (string_input p(cin); p != string_input(); ++p) { cout << "查找输入单词的变位词中..." << endl; string word = *p; sort(word.begin(), word.end()); bool found_one = false; do { if (binary_search(dictionary.begin(), dictionary.end(), word)) { cout << " " << word ; found_one = true; } } while (next_permutation(word.begin(), word.end())); if (!found_one) cout << " 抱歉,没有找到变位词\n"; cout << "\n请输入下一个单词 " << "(或输入Ctrl+Z终止程序 ) \n" << endl; } } int main() { welcome(); vector<string> dictionary; readDict(dictionary); analyseAnagram(dictionary); system("pause"); return 0; }

#include <iostream> #include <fstream> #include <vector> #include <unordered_map> using namespace std; void compress(const string& inputFilePath, const string& outputFilePath) { // 读取输入文件 ifstream inputFile(inputFilePath, ios::binary); // 构建字典 unordered_map<string, int> dictionary; int dictionarySize = 256; for (int i = 0; i < 256; i++) { dictionary[string(1, i)] = i; } vector<int> compressedData; string currentString; char inputChar; while (inputFile.get(inputChar)) { // 获取当前字符串 string newString = currentString + inputChar; // 当前字符串在字典中存在时继续添加字符 if (dictionary.find(newString) != dictionary.end()) { currentString = newString; } // 当前字符串不在字典中时 else { // 将当前字符串添加到压缩数据中 compressedData.push_back(dictionary[currentString]); // 将新的字符串添加到字典中 dictionary[newString] = dictionarySize++; currentString = string(1, inputChar); } } // 处理最后一组字符串 if (!currentString.empty()) { compressedData.push_back(dictionary[currentString]); } // 输出到文件 ofstream outputFile(outputFilePath, ios::binary); for (const int& code : compressedData) { outputFile.write((char*)&code, sizeof(int)); } } int main(int argc, char* argv[]) { if (argc < 3) { cout << "Usage: LZWCompressor <input_file> <output_file>" << endl; } else { string inputFilePath = argv[1]; string outputFilePath = argv[2]; compress(inputFilePath, outputFilePath); } return 0; }如何使用这段代码压缩文件

最新推荐

recommend-type

yolov5-face-landmarks-opencv

yolov5检测人脸和关键点,只依赖opencv库就可以运行,程序包含C++和Python两个版本的。 本套程序根据https://github.com/deepcam-cn/yolov5-face 里提供的训练模型.pt文件。转换成onnx文件, 然后使用opencv读取onnx文件做前向推理,onnx文件从百度云盘下载,下载 链接:https://pan.baidu.com/s/14qvEOB90CcVJwVC5jNcu3A 提取码:duwc 下载完成后,onnx文件存放目录里,C++版本的主程序是main_yolo.cpp,Python版本的主程序是main.py 。此外,还有一个main_export_onnx.py文件,它是读取pytorch训练模型.pt文件生成onnx文件的。 如果你想重新生成onnx文件,不能直接在该目录下运行的,你需要把文件拷贝到https://github.com/deepcam-cn/yolov5-face 的主目录里运行,就可以生成onnx文件。
recommend-type

setuptools-0.6c8-py2.5.egg

文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

5-3.py

5-3
recommend-type

Java八股文.pdf

"Java八股文"是一个在程序员社群中流行的术语,特别是在准备技术面试时。它指的是一系列在Java编程面试中经常被问到的基础知识点、理论概念和技术细节。这个术语的命名来源于中国古代科举考试中的“八股文”,一种具有固定格式和套路的文章形式。 在Java编程的上下文中,"Java八股文"通常包括以下几个方面:"Java八股文"是一个在程序员社群中流行的术语,特别是在准备技术面试时。它指的是一系列在Java编程面试中经常被问到的基础知识点、理论概念和技术细节。这个术语的命名来源于中国古代科举考试中的“八股文”,一种具有固定格式和套路的文章形式。 在Java编程的上下文中,"Java八股文"通常包括以下几个方面:"Java八股文"是一个在程序员社群中流行的术语,特别是在准备技术面试时。它指的是一系列在Java编程面试中经常被问到的基础知识点、理论概念和技术细节。这个术语的命名来源于中国古代科举考试中的“八股文”,一种具有固定格式和套路的文章形式。 在Java编程的上下文中,"Java八股文"通常包括以下几个方面:"Java八股文"是一个在程序员社群中流行的术语,特别是在准备技术面试时。它
recommend-type

麦肯锡咨询顾问必备宝典.ppt

麦肯锡咨询顾问必备宝典.ppt
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。