如何按字符从文件到2D数组读取字符
How to read character by character from a file to a 2D array
我正在尝试从看起来像这样的文本文件中读取字符。" - "应该转换为零,而" x"应该转换为零
3
3
-x-
xx-
--x
我能够使用单独的函数读取前两个整数,但是当我使用此方法复制它时,我将获得一个带有所有零的3x6 2D数组。输出为
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
要从文件中读取字符,您有一些选项。
字符
char c;
ifstream data_file("my_file.txt");
while (data_file >> c)
{
Do_Something_With_Character(c);
}
by text Line
从输入数据示例中,您可能需要读取文本行:
std::string text;
ifstream data_file("my_file.txt")
while (getline(data_file, text))
{
for (int index = 0; index < text.length(); ++i)
{
Do_Something_With_Character(text[index]);
}
}
注意:以上示例是读取数据的通用。他们不解析OP的输入文件。
相关文章:
- 打印第二列时的2d字符矢量打印空间
- 如何打印 2D 字符数组C++
- 在将字符插入 2d 字符串时如何不显示空字符?
- 替换 2D 数组中的字符
- 当字符串为"\0"时如何结束 2D 字符数组?
- C++将 txt 数据和字符读取到 2D 数组中
- 如何在 2D 字符数组中打印 [x][y] 元素的地址?(C++)
- 将字符 2d 数组转换为整数 2d 数组
- 当 ifstream 将字符读入 2D 数组时,会出现奇怪的字符
- 传递动态分配的 2D 字符数组会导致分段错误
- 如何按字符从文件到2D数组读取字符
- 创建一个字符的2D矩阵的问题
- 将字符串与给定的 2d 字符数组匹配
- 如何使用用户输入正确修改 2D 数组中的字符数组元素?专门用于电影院座位
- 全球字符阵列(2D)
- 打印一行 2D 字符数组 (C++)
- 在C++中打印 2D 字符数组
- 尝试在c++中将文本文件(ifstream)转换为2D字符数组
- 限制 2D 数组中的字符输入
- 访问动态 2D 字符数组时引发访问冲突异常