【发布时间】:2017-10-05 18:43:42
【问题描述】:
我正在尝试从包含 4 行标题的数据文件中读取数据,并且还有一个我将存储到 2d int 数组中的数字列表
例如
标题
标题
标题
标题
int
int
int
int
......
我需要以某种方式跳过这些包含文本的标题行,只使用 int 行并将它们存储到上述二维数组中。当我打开文件并搜索它时,由于一开始的文本,它根本不存储任何值。我已经尝试了多个 if 语句和其他方法来解决这个问题,但到目前为止已经奏效了。
int main()
{
ifstream imageFile;
imageFile.open("myfile");
if (!imageFile.is_open())
{
exit (EXIT_FAILURE);
}
int test2[16][16];
int word;
imageFile >> word;
while (imageFile.good())
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
test2[i][j] = word;
imageFile >> word;
}
}
}
【问题讨论】:
-
getline,getline,getline,getline,阅读其余部分。
-
或者总是读取字符串,检查格式,然后跳过或格式化为您需要的数据类型(如果标题行数不同)。
-
这里有两个任务。 1)扔掉前四行,2)读入整数。完成第 1 步后才能执行第 2 步。请执行第 1 步。