将网格9x9数字文本文件写出为与C++格式相同的新文本文件,可以通过以下步骤实现:
以下是一个示例代码,用于将网格9x9数字文本文件写出为与C++格式相同的新文本文件:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main() {
std::ifstream inputFile("input.txt"); // 原始文件输入流
std::ofstream outputFile("output.txt"); // 新文件输出流
if (inputFile.is_open() && outputFile.is_open()) {
std::string line;
std::vector<std::vector<int>> grid; // 二维数组用于存储网格数据
// 逐行读取原始文件,并将数字存储到二维数组中
while (std::getline(inputFile, line)) {
std::vector<int> row;
size_t pos = 0;
std::string token;
while ((pos = line.find(' ')) != std::string::npos) {
token = line.substr(0, pos);
row.push_back(std::stoi(token));
line.erase(0, pos + 1);
}
row.push_back(std::stoi(line));
grid.push_back(row);
}
// 将二维数组中的数字按照C++格式写入新文件
for (const auto& row : grid) {
for (const auto& num : row) {
outputFile << num << " ";
}
outputFile << std::endl;
}
std::cout << "文件写入完成!" << std::endl;
// 关闭文件输入流和文件输出流
inputFile.close();
outputFile.close();
} else {
std::cout << "无法打开文件!" << std::endl;
}
return 0;
}
请注意,上述示例代码仅供参考,具体实现方式可能因实际需求和环境而有所不同。在实际应用中,还需要考虑错误处理、异常情况处理、文件路径等因素。
没有搜到相关的文章