C++ 试图一次读取一个字符的矩阵
我正在尝试读取以下输入,并将矩阵从输入分配到字符矩阵。问题是,当我输出矩阵时,它以一种奇怪的方式输出角色。我做错了什么 输入:C++ 试图一次读取一个字符的矩阵,c++,codeblocks,C++,Codeblocks,我正在尝试读取以下输入,并将矩阵从输入分配到字符矩阵。问题是,当我输出矩阵时,它以一种奇怪的方式输出角色。我做错了什么 输入: 10 10 PPPPPPPPPP PXXXXTPXXP PXPPPPXXXP PXXXPJPPXP PPPXPXXXXP CXXXXXPXPP PXPXXXPXXP PXPPPPPXXP PXXXXXXXXP PPPPPPPPPP 我的代码: #include <iostream> #include <fstream> using names
10 10
PPPPPPPPPP
PXXXXTPXXP
PXPPPPXXXP
PXXXPJPPXP
PPPXPXXXXP
CXXXXXPXPP
PXPXXXPXXP
PXPPPPPXXP
PXXXXXXXXP
PPPPPPPPPP
我的代码:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream in("tom.in");
ofstream out("tom.out");
int n, m;
char s[101][101];
in>>n>>m; //read the size of the matrix
for(int i = 1; i<=n; i++)
for(int j = 1; j<=m; j++)
in.get(s[i][j]); //assign each element from the file to the matrix
for(int i = 1; i<=n; i++)
{
out<<endl;
for(int j = 1; j<=m; j++)
out<<s[i][j];
}
return 0;
}
我使用的IDE是代码块。替换
in.get(s[i][j]);
与
说明:
让我们通过准确打印出读取的内容来了解情况,但作为数字,我们可以与ASCII表进行比较:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream in("tom.in");
ofstream out("tom.out");
int n, m;
char x;
in>>n>>m; //read the size of the matrix
for(int i = 1; i<=n; i++)
{
for(int j = 1; j<=m; j++)
{
in.get(x);
cout << (int)x << ' ';
}
cout << '\n';
}
return 0;
}
现在输出看起来像
80 80 80 80 80 80 80 80 80 80
80 88 88 88 88 84 80 88 88 80
80 88 80 80 80 80 88 88 88 80
80 88 88 88 80 74 80 80 88 80
80 80 80 88 80 88 88 88 88 80
67 88 88 88 88 88 80 88 80 80
80 88 80 88 88 88 80 88 88 80
80 88 80 80 80 80 80 88 88 80
80 88 88 88 88 88 88 88 88 80
80 80 80 80 80 80 80 80 80 80
80 80 80 80 80 80 80 80 80 80
80 88 88 88 88 84 80 88 88 80
80 88 80 80 80 80 88 88 88 80
80 88 88 88 80 74 80 80 88 80
80 80 80 88 80 88 88 88 88 80
67 88 88 88 88 88 80 88 80 80
80 88 80 88 88 88 80 88 88 80
80 88 80 80 80 80 80 88 88 80
80 88 88 88 88 88 88 88 88 80
80 80 80 80 80 80 80 80 80 80
当转换回ASCII字符时,它是预期的
PPPPPPPPPP
PXXXXTPXXP
PXPPPPXXXP
PXXXPJPPXP
PPPXPXXXXP
CXXXXXPXPP
PXPXXXPXXP
PXPPPPPXXP
PXXXXXXXXP
PPPPPPPPPP
PPPPPPPPPP
PXXXXTPXXP
pxpppxxxp
PXXXPJPPXP
pppxxxp
CXXXPXPP
PXXXPXXP
pxppppxp
PXXXXXXXXXP
pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp
替换
in.get(s[i][j]);
与
说明:
让我们通过准确打印出读取的内容来了解情况,但作为数字,我们可以与ASCII表进行比较:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream in("tom.in");
ofstream out("tom.out");
int n, m;
char x;
in>>n>>m; //read the size of the matrix
for(int i = 1; i<=n; i++)
{
for(int j = 1; j<=m; j++)
{
in.get(x);
cout << (int)x << ' ';
}
cout << '\n';
}
return 0;
}
现在输出看起来像
80 80 80 80 80 80 80 80 80 80
80 88 88 88 88 84 80 88 88 80
80 88 80 80 80 80 88 88 88 80
80 88 88 88 80 74 80 80 88 80
80 80 80 88 80 88 88 88 88 80
67 88 88 88 88 88 80 88 80 80
80 88 80 88 88 88 80 88 88 80
80 88 80 80 80 80 80 88 88 80
80 88 88 88 88 88 88 88 88 80
80 80 80 80 80 80 80 80 80 80
80 80 80 80 80 80 80 80 80 80
80 88 88 88 88 84 80 88 88 80
80 88 80 80 80 80 88 88 88 80
80 88 88 88 80 74 80 80 88 80
80 80 80 88 80 88 88 88 88 80
67 88 88 88 88 88 80 88 80 80
80 88 80 88 88 88 80 88 88 80
80 88 80 80 80 80 80 88 88 80
80 88 88 88 88 88 88 88 88 80
80 80 80 80 80 80 80 80 80 80
当转换回ASCII字符时,它是预期的
PPPPPPPPPP
PXXXXTPXXP
PXPPPPXXXP
PXXXPJPPXP
PPPXPXXXXP
CXXXXXPXPP
PXPXXXPXXP
PXPPPPPXXP
PXXXXXXXXP
PPPPPPPPPP
PPPPPPPPPP
PXXXXTPXXP
pxpppxxxp
PXXXPJPPXP
pppxxxp
CXXXPXPP
PXXXPXXP
pxppppxp
PXXXXXXXXXP
PPPPPPPPPP
您可以先读取多维数组的长度,然后迭代填充数组的所有内容
std::ifstream in("data.txt");
std::ofstream out("tom.txt");
int n, m;
// verify the lengths
in >> m >> n;
std::cout << n << ", " << m << std::endl;
char** data = new char*[n];
for(auto i(0); i != n; ++i)
data[i] = new char[m];
for(auto i(0); i != n; ++i)
for(auto j(0); j != m; ++j)
in >> data[i][j];
for(auto i(0); i != n; ++i){
for(auto j(0); j != m; ++j)
std::cout << data[i][j]; // just to print to the screen. you can write to the second file with `out` object.
std::cout << std::endl;
}
// clean
for(auto i(0); i != n; ++i)
delete[]data[i];
delete[]data;
// close streams
in.close();
out.close();
std::ifstream-in(“data.txt”);
std::of stream out(“tom.txt”);
int n,m;
//验证长度
在>>m>>n;
std::cout您可以先读取多维数组的长度,然后迭代填充数组的所有内容
std::ifstream in("data.txt");
std::ofstream out("tom.txt");
int n, m;
// verify the lengths
in >> m >> n;
std::cout << n << ", " << m << std::endl;
char** data = new char*[n];
for(auto i(0); i != n; ++i)
data[i] = new char[m];
for(auto i(0); i != n; ++i)
for(auto j(0); j != m; ++j)
in >> data[i][j];
for(auto i(0); i != n; ++i){
for(auto j(0); j != m; ++j)
std::cout << data[i][j]; // just to print to the screen. you can write to the second file with `out` object.
std::cout << std::endl;
}
// clean
for(auto i(0); i != n; ++i)
delete[]data[i];
delete[]data;
// close streams
in.close();
out.close();
std::ifstream-in(“data.txt”);
std::of stream out(“tom.txt”);
int n,m;
//验证长度
在>>m>>n;
std::cout关于在这里,不必发生一些有趣的事情<代码> > >代码>。尝试在C++中伪造基于1的数组总是导致错误、不符合边界的访问,或者错误地使用数组的<代码> 0 < /C>索引。只使用C++中的数组,即使用从<代码> 0 >代码>到<代码> n-1</CO的索引。de>,其中n
是条目数。我试图解决的编程问题要求我将它们从1索引到n,这就是我这么做的原因。此外,即使我将其从0索引,我也不认为它可以解决在这里,不必发生一些有趣的事情<代码> > >代码>。尝试在C++中伪造基于1的数组总是导致错误、不符合边界的访问,或者错误地使用数组的<代码> 0 < /C>索引。只使用C++中的数组,即使用从<代码> 0 >代码>到<代码> n-1</CO的索引。de>,其中n
是条目数。我试图解决的编程问题要求我将它们从1索引到n,这就是我这么做的原因。此外,即使我从0索引,我也不认为它能解决问题。如果你想读取空白,请返回get
,或者如果我返回“get”,它会弄乱垫子再说一遍,所以我想如果你想要空格而不是行尾,我会选择“noskipws”,查看std::getline
读取整行内容,然后按照你想要的方式将其拆分。std::istringstream
在这里会很有帮助。如果你想读取空格,请返回get
,或者如果我返回“get”,它会弄乱矩阵同样,如果你想要空格而不是行的结尾,我想我会选择“noskipws”,查看std::getline
阅读整行内容,然后按照你想要的方式将其拆分。std::istringstream
在这里会有所帮助。