从没有空格的文本文件中读取数字
Read number from text file without whitespace
我试图从文本文件中读取12位数字到数组。如果我在每个数字之间放置空格,我已经能够成功地做到这一点。例如:
1 1 1 1 1 1 1 1 1 1 1 1
但是当我删除数字之间的空格时,我的程序不再能够从文本文件中分配数组。例如:
111111111111
我确信答案很简单,但是我在任何地方都找不到解决我确切问题的方法。下面是我用来分配数组的while循环。
void int_class::allocate_array(std::ifstream& in, const char* file)
{
//open file
in.open(file);
//read file in to array
int i = 0;
while( !in.eof())
{
in >> myarray[i];
i++;
}
in.close();
}
要读取一个字符数组,假设没有空格或其他分隔符,您可以一次从输入流中读取整个字符数组:
in >> myarray;
要创建一个整数数组,您可以一个字符一个字符地读取输入,并在适当的位置填充数组:
char c;
int i = 0;
while( !in.eof())
{
in >> c;
myarray[ i++ ] = c - '0';
}
在这种情况下,可以在任何地方有任意数量的空格,它们将被忽略。
相关文章:
- 正在将csv文件读取为双精度矢量
- 将从格式化文本文件读取的文本数据存储到链表
- C++ 读取文件读取文件不正确
- 使用对象文件读取三角形数据网格
- 如何减慢从 BVH 文件读取的 opengl 动画?
- 从文件读取,并写入函数C++中的数组
- 从对象C++中的文件读取内容时出现分段错误
- 从文件读取并通过矢量输出到控制台
- 尝试将字符串从文件读取到无符号字符向量中
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 简化文件读取的函数
- 如何将逗号分隔的文件读取为 2D int 数组?
- 在 c++ 中将数据从文件读取到队列中
- 'std::wstring_convert'尽可能多地转换(从 UTF8 文件读取块)
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 文件读取C++包括空格键
- 从文件读取时未将值推回矢量
- 从文件 C++ 读取时的无限循环
- 从同一文件读取和写入?
- 从.txt文件读取到C++中的双精度向量