尝试在c++中将文本文件(ifstream)转换为2D字符数组

Trying to convert a text file (ifstream) into a 2D array of chars in c++

本文关键字:转换 ifstream 2D 数组 字符 文件 c++ 文本      更新时间:2023-10-16

我已经试了大约两天了,我想我已经接近了,但仍然没有雪茄!我有一个文本文件,看起来像这样:

++++++
+H   +
+    +
+    +
+   X+
++++++

如果我试着正常打印出来,一切都正常:

void Level::LevelPrinter()  //This prints out fine
{
inputMap.open("Level1.txt");
string input;
while (getline(inputMap, input))
{
cout << input << endl;
}
}

但当我试图将所有这些符号放入2D数组时,我要么在我尝试的某些形式的代码中得到空格;或者,在我最近的一次尝试中,似乎最接近我的需求,我把奇怪的符号放在了正确的位置。我不知道怎么了。。。

这是我现在正在尝试的代码:

void Level::LevelPrinterArray()
{
inputMap.open("Level1.txt");
char MapSymbols;
int Rows, Cols;
{
for (Rows = 0; Rows < 6; Rows++)
{
for (Cols = 0; Cols < 6; Cols++)
{
inputMap >> MapSymbols;
MapLayoutArray[Rows][Cols] = MapSymbols;
cout << MapSymbols;
}
cout << endl;
}
}

}

这就是控制台向我展示的内容:

╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠

顺便说一句,MapLayoutArray只是:

char MapLayoutArray[6][6];

RadLexus,我试着不使用>>,所以它看起来像这样:

void Level::LevelPrinterArray()
{
inputMap.open("Level1.txt");
char MapSymbols;
int Rows, Cols;
{
for (Rows = 0; Rows < 6; Rows++)
{
for (Cols = 0; Cols < 6; Cols++)
{
inputMap.get(MapSymbols);
MapLayoutArray[Rows][Cols] = MapSymbols;
cout << MapSymbols;
}
cout << endl;
}
}

}

还打印出:

╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠

-----更新------

在第一个代码上使用.close(我说的那个代码工作正常)。使第二个代码中的字符显示为正常字符!问题是,它们现在看起来是这样的:

++++++
+H
+
+
+
+
+
+
X+
+

玩了一点代码,现在这个代码:

void Level::LevelPrinterArray()
{
inputMap.open("Level1.txt");
char MapSymbols;
{
for (int Rows = 0; Rows < 6; Rows++)
{
for (int Cols = 0; Cols < 6; Cols++)
{
inputMap.get(MapSymbols);
if (MapSymbols != 'n')
{
MapLayoutArray[Rows][Cols] = MapSymbols;
cout << MapSymbols;
}
else
{
cout << endl;
}   
}
}
}
inputMap.close();
cout << endl;
}

导致以下情况:

++++++
+H   +
+    +
+    +
+   X+
+
Press any key to continue . . .

所以我非常接近,但我无法打印最后一行。我尝试了很多方法让它打印最后一行,比如制作第一个for循环"Rows<=6",但这正确地打印出了最后一行并导致控制台崩溃。。。我会再玩一些。。。如果你们有什么想法,请告诉我。如果我弄清楚了,我会在这里更新。。。

我试着在电脑上解决你的问题。

我所做的更改是迭代当前行,并将其逐个字符插入数组

这个代码运行得很好:

#include <string>
#include <iostream>
#include <fstream>
int main(){
std::ifstream inputMap("C:\Res\Level1.txt");
char arr[6][6];
for (int i = 0; i < 6; i++)
{
std::string input;
std::getline(inputMap, input);
for (int k = 0; k < input.length(); k++)
{
arr[i][k] = input[k];
std::cout << input[k];
}
std::cout << std::endl;
}
std::cout << std::endl;
for (size_t l = 0; l < 6; l++)
{
for (size_t m = 0; m < 6; m++)
{
std::cout << arr[l][m];
}
std::cout << std::endl;
}
system("PAUSE");
return 0;
}

输出是所需的(我在这里复制了它)。

出于您的目的,请不要在第一次或第二次打印;)

++++++
+H   +
+    +
+    +
+   X+
++++++
++++++
+H   +
+    +
+    +
+   X+
++++++

第一次尝试

inputMap >> MapSymbols;

运算符>>忽略所有白色字符,包括空格。

第二次和第三次尝试

inputMap.get(MapSymbols);

使用正确的函数来考虑空格,但您忘记了,当您获得换行符'n'时,您不能等待下一次迭代来读取(好的)下一个字符。根据操作系统的不同,您可能还会遇到'r'

解决方案

您可以使用std::ws跳过每行末尾的这些空白1:

for (std::size_t Rows = 0; Rows < 6; Rows++)
{
for (std::size_t Cols = 0; Cols < 6; Cols++)
{
char MapSymbols;
inputMap.get(MapSymbols);
MapLayoutArray[Rows][Cols] = MapSymbols;
std::cout << MapSymbols;
}
inputMap >> std::ws; // HERE
std::cout << std::endl;
}

1假设行不是以空格开头的,否则使用ignore()成员函数。