读取文件中的数字数量无限,存储在数组中(无空间)

Read an indefinite number of digits from a file, store in an array (no space)

本文关键字:存储 数组 无空间 无限 文件 数字 读取      更新时间:2023-10-16

我想读一个包含此(示例)的文件:

194920392038122341

并将每个数字存储在整数阵列中。例如,a [0]为1,[1]为9,等等。

我尝试使用这样的东西读取数字并将其存储在数组中:

    int n = 0;
    vector<int> numbers; 
    ifstream in
    in.open("input.txt");
    while (in >> n)
    {
        numbers.push_back(n);
    }
    int* array = &numbers[0];

但是,由于某种原因,这不会读取文件中的个人数字。它读为" 12"," 34"," 35"等。

这就是我的方式:

char n = 0;
vector<int> numbers; 
ifstream in("input.txt");
while (in.get(n)) {
   if(std::isdigit(n) {
       numbers.push_back(n - '0');
   }
}
int* array = &numbers[0];