用C++从txt文件中读取二维数组 [英] Read 2d array from txt file in c++
本文介绍了用C++从txt文件中读取二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
char arr1[10][10];
cout << "Reading Start" << endl;
ifstream rfile("test.txt");
rfile.getline(arr1[10], 10);
int i, j;
for (i = 0; i < 6; i++)
{
for (j = 0; i < 6; j++)
{
cout << arr1[i][j];
}
}
cout << "
Read Done" << endl << endl;
rfile.close();
}
这是我的test.txt文件
0 4 7 0 0 0
4 0 0 5 3 0
7 0 0 0 6 0
0 5 3 0 0 2
0 3 4 0 0 2
0 0 0 2 2 0
我想读这个矩阵,但当使用上面的代码时,它显示核心转储输出,有人能给我一个更好的解决方案来做这件事吗?
推荐答案
有很多其他方法可以执行特定任务,但我想您的方法没有错,您只是在第二个for循环条件中犯了一个简单的键入错误。所以我会为你修复你的代码。 您也可以一次只输入一个值。
#include<bits/stdc++.h>
using namespace std;
int main()
{
char arr1[10][10];
cout <<"Reading Start" <<endl;
ifstream rfile("test.txt");
int i,j;
for(i=0;i<6;i++){
for(j=0;j<6;j++){
rfile >> arr1[i][j];
cout << arr1[i][j] << " ";
}
cout << endl;
}
cout <<"
Read Done" <<endl<<endl;
rfile.close();
}
这篇关于用C++从txt文件中读取二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文