C++ 将二维数组保存到文件c++;
我试图将二维数组保存到文件中,但它显示为“^L”。如果用户输入Y或Y,则程序应该结束,但它将打印我的2D数组C++ 将二维数组保存到文件c++;,c++,arrays,file,multidimensional-array,C++,Arrays,File,Multidimensional Array,我试图将二维数组保存到文件中,但它显示为“^L”。如果用户输入Y或Y,则程序应该结束,但它将打印我的2D数组 //Allow user to quit cout << "Would you like to quit this game? Enter Y or N: " << endl; cin >> Choice; if (Choice == 'Y' || Choice == 'y') { ofstream outfile("Map.txt");
//Allow user to quit
cout << "Would you like to quit this game? Enter Y or N: " << endl;
cin >> Choice;
if (Choice == 'Y' || Choice == 'y')
{
ofstream outfile("Map.txt");
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
outfile << "Here is your map! " << Map[ROWS][COLS] << endl;
}
outfile.close();
}
if (Choice == 'N' || Choice == 'n')
{
// Add code to play the game
PlayTurn(TreasureR, TreasureC, Row, Col, NumMoves);
}
// Print the map for true
PrintMap(Map,true, TreasureR, TreasureC, StartR, StartC, Row, Col);
//End the game
cout << "You finished the Game in " << NumMoves <<" moves.\n";
//允许用户退出
不能选择;
如果(选项='Y'| |选项=='Y')
{
流出流文件(“Map.txt”);
对于(int row=0;row outfile您所做的工作称为序列化。
你有两个选择
使用类似boost的库为您完成此操作
让你拥有
因为你的要求很简单,我会提出我自己的要求
std::ostream& serialize(std::ostream& outfile, int** arr, int rows, int cols) {
outfile << rows << " ";
outfile << cols << " ";
for (int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
outfile << arr[i][j] << " ";
return outfile;
}
int** deserialize(std::istream& file, int& rows, int& cols) {
file >> rows;
file >> cols;
int** arr = new int*[rows];
for (int i = 0; i < rows; i++) {
arr[i] = new int[cols];
for(int j = 0; j < cols; j++)
file >> arr[i][j];
return arr;
}
std::ostream和序列化(std::ostream和outfile,int**arr,int行,int列){
输出文件arr[i][j];
返回arr;
}
此代码未经编译或测试!您所做的工作称为序列化。
你有两个选择
使用类似boost的库为您完成此操作
让你拥有
因为你的要求很简单,我会提出我自己的要求
std::ostream& serialize(std::ostream& outfile, int** arr, int rows, int cols) {
outfile << rows << " ";
outfile << cols << " ";
for (int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
outfile << arr[i][j] << " ";
return outfile;
}
int** deserialize(std::istream& file, int& rows, int& cols) {
file >> rows;
file >> cols;
int** arr = new int*[rows];
for (int i = 0; i < rows; i++) {
arr[i] = new int[cols];
for(int j = 0; j < cols; j++)
file >> arr[i][j];
return arr;
}
std::ostream和序列化(std::ostream和outfile,int**arr,int行,int列){
输出文件arr[i][j];
返回arr;
}
此代码未经编译或测试!您所做的工作称为序列化。
你有两个选择
使用类似boost的库为您完成此操作
让你拥有
因为你的要求很简单,我会提出我自己的要求
std::ostream& serialize(std::ostream& outfile, int** arr, int rows, int cols) {
outfile << rows << " ";
outfile << cols << " ";
for (int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
outfile << arr[i][j] << " ";
return outfile;
}
int** deserialize(std::istream& file, int& rows, int& cols) {
file >> rows;
file >> cols;
int** arr = new int*[rows];
for (int i = 0; i < rows; i++) {
arr[i] = new int[cols];
for(int j = 0; j < cols; j++)
file >> arr[i][j];
return arr;
}
std::ostream和序列化(std::ostream和outfile,int**arr,int行,int列){
输出文件arr[i][j];
返回arr;
}
此代码未经编译或测试!您所做的工作称为序列化。
你有两个选择
使用类似boost的库为您完成此操作
让你拥有
因为你的要求很简单,我会提出我自己的要求
std::ostream& serialize(std::ostream& outfile, int** arr, int rows, int cols) {
outfile << rows << " ";
outfile << cols << " ";
for (int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
outfile << arr[i][j] << " ";
return outfile;
}
int** deserialize(std::istream& file, int& rows, int& cols) {
file >> rows;
file >> cols;
int** arr = new int*[rows];
for (int i = 0; i < rows; i++) {
arr[i] = new int[cols];
for(int j = 0; j < cols; j++)
file >> arr[i][j];
return arr;
}
std::ostream和序列化(std::ostream和outfile,int**arr,int行,int列){
输出文件arr[i][j];
返回arr;
}
此代码未编译或测试!可以执行类似操作将映射序列化为流。流可以由您指定。std::cout
或std::fstream
可以工作
#include <iostream>
#include <fstream>
template<typename T, int height, int width>
std::ostream& writemap(std::ostream& os, T (&map)[height][width])
{
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
os << map[i][j]<<" ";
}
os<<"\n";
}
return os;
}
int main()
{
const int width = 4;
const int height = 5;
int map[height][width] =
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16},
{17, 18, 19, 20}
};
std::fstream of("Map.txt", std::ios::out | std::ios::app);
if (of.is_open())
{
writemap(of, map);
writemap(std::cout, map);
of.close();
}
}
可以执行类似操作将映射序列化为流。流可以由您指定。std::cout
或std::fstream
可以工作
#include <iostream>
#include <fstream>
template<typename T, int height, int width>
std::ostream& writemap(std::ostream& os, T (&map)[height][width])
{
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
os << map[i][j]<<" ";
}
os<<"\n";
}
return os;
}
int main()
{
const int width = 4;
const int height = 5;
int map[height][width] =
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16},
{17, 18, 19, 20}
};
std::fstream of("Map.txt", std::ios::out | std::ios::app);
if (of.is_open())
{
writemap(of, map);
writemap(std::cout, map);
of.close();
}
}
可以执行类似操作将映射序列化为流。流可以由您指定。std::cout
或std::fstream
可以工作
#include <iostream>
#include <fstream>
template<typename T, int height, int width>
std::ostream& writemap(std::ostream& os, T (&map)[height][width])
{
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
os << map[i][j]<<" ";
}
os<<"\n";
}
return os;
}
int main()
{
const int width = 4;
const int height = 5;
int map[height][width] =
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16},
{17, 18, 19, 20}
};
std::fstream of("Map.txt", std::ios::out | std::ios::app);
if (of.is_open())
{
writemap(of, map);
writemap(std::cout, map);
of.close();
}
}
可以执行类似操作将映射序列化为流。流可以由您指定。std::cout
或std::fstream
可以工作
#include <iostream>
#include <fstream>
template<typename T, int height, int width>
std::ostream& writemap(std::ostream& os, T (&map)[height][width])
{
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
os << map[i][j]<<" ";
}
os<<"\n";
}
return os;
}
int main()
{
const int width = 4;
const int height = 5;
int map[height][width] =
{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16},
{17, 18, 19, 20}
};
std::fstream of("Map.txt", std::ios::out | std::ios::app);
if (of.is_open())
{
writemap(of, map);
writemap(std::cout, map);
of.close();
}
}
还有其他方法吗?我在编程课上还没有学过序列化。@savannaalexis没有什么。序列化只是指将数据结构存储到文本文件中。我只是不认为这是我的教授想要的。我们只是刚刚学过文件。@savannaalexis没有其他方法来保存array到一个文件。你必须迭代数组并保存数组中的所有值。还有其他方法吗?我还没有在编程课上学习过序列化。@savannaalexis没有什么。序列化只意味着将数据结构存储到文本文件中。我只是不认为这是我的教授想要的。我们有ly刚刚学习了有关文件的知识。@savannaalexis没有其他方法将数组保存到文件中。您必须迭代数组并保存数组中的所有值。还有其他方法吗?我还没有在编程类中学习过序列化。@savannaalexis没有任何内容。序列化只意味着将数据结构存储到text文件。我只是不认为这是我的教授想要的。我们只是刚刚了解了文件。@savannaalexis没有其他方法将数组保存到文件中。你必须迭代数组并保存数组中的所有值。还有其他方法吗?我在编程课上没有学习序列化。@savannaalex没有什么。序列化只是指将数据结构存储到文本文件中。我只是不认为这是我的教授想要的。我们只是刚刚了解了文件。@savannaalexis没有其他方法将数组保存到文件中。你必须迭代数组并保存数组中的所有值。