需要以相同的格式 C++ 将网格 9x9 数字文本文件写出到新的文本文件
Need to write out grid 9x9 number text file to a new text file in the same format C++
This is the input file that my output file needs to be the same as
this is the output I currently have (its getting closer)
我完成的问题的第一部分是编写一个方法,将 9x9 数字的网格文本文件放入一个二维数组中,并将文本文件的每一行保存到每个内存槽中,而另一个槽该数组包含每个插槽中的数字列。这是显示的代码:
void Grid::LoadGrid(char const * const)
{
int m_grid[9][9];
ifstream fp ("Grid1.txt");
for (int x = 0; x < 9; x++)
{
for(int y =0; y < 9; y++)
{
fp >> m_grid[x][y];
}
}
for (int x = 0; x < 9; x++)
{
for (int y = 0; y < 9; y++)
{
cout << m_grid[x][y] << " ";
}
cout << endl;
return;
fp.close();
}
}
效果很好。第二个问题是现在获取该数组并将其输出到一个新的文本文件中,我称之为 "GridOut.txt" 这是它的代码:
void Grid::SaveGrid(char const * const)
{
int m_grid[9][9];
ifstream fp("Grid1.txt");
ofstream fout("GridOut.txt");
for (int x = 0; x < 9; x++)
{
for (int y = 0; y < 9; y++)
{
fp >> m_grid[x][y];
fout << m_grid[x][y];
}
}
for (int x = 0; x < 9; x++)
{
for (int y = 0; y < 9; y++)
{
fout << m_grid[x][y] << " ";
}
fout << endl;
}
}
这是在创建一个新文件并将值放入其中,但它不是预期的 9x9 网格格式。
抱歉我的英语不好,希望它的解释足够好让人们理解。
fout << m_grid[x][y] << endl;
这会在每个数字后添加一个换行符。您应该将其替换为 space:
fout << m_grid[x][y] << " ";
而不是 cout << endl
做 fout << endl
.
这是结果:
void Grid::SaveGrid(char const* const) {
int m_grid[9][9];
ifstream fp("Grid1.txt");
ofstream fout("GridOut.txt");
for (int x = 0; x < 9; x++) {
for (int y = 0; y < 9; y++) {
fp >> m_grid[x][y];
}
}
for (int x = 0; x < 9; x++) {
for (int y = 0; y < 9; y++) {
fout << m_grid[x][y] << " ";
}
fout << endl;
}
}
This is the input file that my output file needs to be the same as
this is the output I currently have (its getting closer)
我完成的问题的第一部分是编写一个方法,将 9x9 数字的网格文本文件放入一个二维数组中,并将文本文件的每一行保存到每个内存槽中,而另一个槽该数组包含每个插槽中的数字列。这是显示的代码:
void Grid::LoadGrid(char const * const)
{
int m_grid[9][9];
ifstream fp ("Grid1.txt");
for (int x = 0; x < 9; x++)
{
for(int y =0; y < 9; y++)
{
fp >> m_grid[x][y];
}
}
for (int x = 0; x < 9; x++)
{
for (int y = 0; y < 9; y++)
{
cout << m_grid[x][y] << " ";
}
cout << endl;
return;
fp.close();
}
}
效果很好。第二个问题是现在获取该数组并将其输出到一个新的文本文件中,我称之为 "GridOut.txt" 这是它的代码:
void Grid::SaveGrid(char const * const)
{
int m_grid[9][9];
ifstream fp("Grid1.txt");
ofstream fout("GridOut.txt");
for (int x = 0; x < 9; x++)
{
for (int y = 0; y < 9; y++)
{
fp >> m_grid[x][y];
fout << m_grid[x][y];
}
}
for (int x = 0; x < 9; x++)
{
for (int y = 0; y < 9; y++)
{
fout << m_grid[x][y] << " ";
}
fout << endl;
}
}
这是在创建一个新文件并将值放入其中,但它不是预期的 9x9 网格格式。
抱歉我的英语不好,希望它的解释足够好让人们理解。
fout << m_grid[x][y] << endl;
这会在每个数字后添加一个换行符。您应该将其替换为 space:
fout << m_grid[x][y] << " ";
而不是 cout << endl
做 fout << endl
.
这是结果:
void Grid::SaveGrid(char const* const) {
int m_grid[9][9];
ifstream fp("Grid1.txt");
ofstream fout("GridOut.txt");
for (int x = 0; x < 9; x++) {
for (int y = 0; y < 9; y++) {
fp >> m_grid[x][y];
}
}
for (int x = 0; x < 9; x++) {
for (int y = 0; y < 9; y++) {
fout << m_grid[x][y] << " ";
}
fout << endl;
}
}