0

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
2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by vijayan121
0
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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.