将这段代码升级成可以直接读取整篇内容 :#include <iostream> #include <fstream> #include <string> #include <map> #include <SimpleIni.h> using namespace std; int main() { map<string, string> config; CSimpleIniA ini; ini.LoadFile("config.ini"); string filename = ini.GetValue("Section1", "filename", ""); string txt = ini.GetValue("Section1", "txt", ""); config["filename"] = filename; config["txt"] = txt; ofstream file(config["filename"]); if (file.is_open()) { file << config["txt"]; file.close(); cout << "文件已成功生成!" << endl; } else { cout << "无法创建文件。" << endl; } return 0; }
时间: 2023-06-18 10:04:16 浏览: 201
可以使用以下代码来实现:
```
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <SimpleIni.h>
using namespace std;
int main() {
map<string, string> config;
CSimpleIniA ini;
ini.LoadFile("config.ini");
string filename = ini.GetValue("Section1", "filename", "");
string txt = "";
ifstream file(txt);
if (file.is_open()) {
string line;
while (getline(file, line)) {
txt += line + "\n";
}
file.close();
} else {
cout << "无法打开文件。" << endl;
return 1;
}
config["filename"] = filename;
config["txt"] = txt;
ofstream outfile(config["filename"]);
if (outfile.is_open()) {
outfile << config["txt"];
outfile.close();
cout << "文件已成功生成!" << endl;
} else {
cout << "无法创建文件。" << endl;
return 1;
}
return 0;
}
```
主要的区别是在读取文件内容时使用了 `ifstream` 对象,并且在读取每一行时都将其加入到了 `txt` 变量中。然后将读取到的文件内容存放在 `config` 字典中。最后,将 `config["txt"]` 的内容写入到 `config["filename"]` 所指定的文件中。
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![text/plain](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)