Hello all,

I am attempting to write a program that will read in, from a file, an unknown number of 4 digit integers into an array (not more than 150 numbers). From there I will be performing various calculations on those numbers.

Listed below is what I have for reading in and displaying the file as a string. Any suggestions on what I should do to read in and initialize file as an array?

ifstream infile ("a:\\crypnums.txt");
 
int main()
{
 time_t t;
 time(&t);
 yl;
 cls;
 
 string numbers, num;
 
 while (! infile)
 {
        cout << "Error opening file  " << endl;
        exit(1);
  }
  
  while (getline (infile, numbers))
  {
        
        string num = numbers;
        cout << num << endl;
  }
  
        
 frz;
 return 0;
} // end of main function
ifstream file("a:\\crypnums.txt");
vector<int> numbers ; 
int i ;
while( file >> i ) numbers.push_back(i) ;
assert( file.eof() ) ;

or

ifstream file("a:\\crypnums.txt");
vector<int> numbers ; 
istream_iterator<int> begin(file), end ;
copy( begin, end, back_inserter(numbers) ) ;
assert( file.eof() ) ;

both assume that the integers are seperated by one or more whitespaces.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.