我剛開始一個小項目,讀取TXT文件是這樣的:如何從一個txt文件中讀取一個迷宮,並把它變成二維數組
4
XSXX
X X
XX X
XXFX
所以我的問題是怎麼看這一點,並把迷宮到C++中的二維字符數組。我試圖使用'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++;
}
顯示你寫的內容並解釋它是如何不足的。 –
不要問_「給我的代碼plz」_,顯示你已經做了什麼,爲什麼它不適合你首先請。 –
我剛剛編輯了我的問題。 – Syrenthia