Mil3d Mil3d - 3 years ago 135
C++ Question

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

I want to read a file that contains this (example):


and store each digit in an integer array. For instance, a[0] would be 1, a[1] would be 9, etc.

I tried using something like this to read the digits and store them in an array:

int n = 0;
vector<int> numbers;

ifstream in"input.txt");

while (in >> n)

int* array = &numbers[0];

However, for some reason, this doesn't read the individual digits from the file. It reads them as "12", "34", "35" etc.

Answer Source

Here's how I would do it:

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];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download