我刚开始做一个小项目,它可以读取像这样的txt文件:
所以我的问题是如何读取这个迷宫并将其放入C ++的2D字符数组中。我尝试使用'getline',但只是让我的代码更复杂了。你知道是否有一种简单的方法来解决这个问题吗?
4
XSXX
X X
XX X
XXFX
所以我的问题是如何读取这个迷宫并将其放入C ++的2D字符数组中。我尝试使用'getline',但只是让我的代码更复杂了。你知道是否有一种简单的方法来解决这个问题吗?
char temp;
string line;
int counter = 0;
bool isOpened=false;
int size=0;
ifstream input(inputFile);//can read any file any name
// i will get it from user
if(input.is_open()){
if(!isOpened){
getline(input, line);//iterater over every line
size= atoi(line.c_str());//atoi: char to integer method.this is to generate the size of the matrix from the first line
}
isOpened = true;
char arr2[size][size];
while (getline(input, line))//while there are lines
{
for (int i = 0; i < size; i++)
{
arr2[counter][i]=line[i];//decides which character is declared
}
counter++;
}