How to stop reading numbers from a file? Example I have 30 numbers in file, maximum size I give is 100. The program still read until 100 although it has only 30 numbers.
1 2 3 4 5 6
while (inputFile>>number){
allNumbers[i]=number;
i++;
}
I read numbers from .txt file, when it reads, it read other lines that have no numbers. The maximums size of numbers is 100.
This is output: 3 54 34 56 67 78 45 3 45 78 90 45 23 98 99 45 45 56 4 54 27 47 85 35 65 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2088042628 1321425858 1587563216 32767 1587562944 32767 1587562920 32767 1587562960 32767 1 0 1587562896 32767 247734857 1 1587562920 32767 1587562920 32767 1587562944 32767 18853888 1 0 1 247731488 1 247731392 1 0 0 0 0 0 0 0 0 0 0
But I want it reads these numbers 3 54 34 56 67 78 45 3 45 78 90 45 23 98 99 45 45 56 4 54 27 47 85 35 65 and stops. Next lines don't have any numbers.
Here is some of my code:
void fromFile(int allNumbers[], int number){
char ch;
string inFile;
ifstream inputFile;
int i=0;
cout << "\n\t Please enter the file name you want to open: ";
cin>>ws; getline(cin, inFile, '\n');
inputFile.open(inFile);
while (inputFile.fail()) {
cout << "\t Failed to open the file. Try again: ";
getline(cin, inFile, '\n');
inputFile.open(inFile);
}
while (inputFile>>number){
allNumbers[i]=number;
i++;
}
inputFile.close();
}
void displayInfo(int num[], int size){
cout <<"\nHere are all the numbers: \n";
for (int i=0; i<size; i++){
cout <<num[i]<<" ";
}
}
if you use a c-style array then you'd have to set its size just so that it can read all the numbers, no more, no less. Fewer array cells than numbers and you'd get an out of bound error and more array cells than numbers (as in your example) and you'd get garbage output in the additional cells.
So your options are (a) set the array size according to the data to be read in, or (b) even better, use std::vector
void fromFile(vector<int>&allNumbers, int number){
string inFile;
ifstream inputFile;
int i=0;
cout << "\n\t Please enter the file name you want to open: ";
cin>>ws; getline(cin, inFile, '\n');
inputFile.open(inFile);
while (inputFile.fail()) {
cout << "\t Failed to open the file. Try again: ";
getline(cin, inFile, '\n');
inputFile.open(inFile);
}
while (inputFile>>number){
allNumbers.push_back(number);
}
inputFile.close();
}
But I only want to use array as I showed in the first one above, I searched online, saw someone use eof() to stop reading file when there are no numbers, but my code doesn't work at all.
But I only want to use array as I showed in the first one above, I searched online, saw someone use eof() to stop reading file when there are no numbers, but my code doesn't work at all.
In your case, there are more numbers, just not numbers you want to read. So, relying on the code to stop when there are no more numbers, when you know there are more numbers seems kind of silly to me.
It isn't entirely clear what you want to happen. Is it always a certain amount of numbers you want to read? Is it only the numbers on a certain line? What is the format of the input file? These things would help you get cogent answers. The container type seems like a secondary consideration.
I want it stops at the last number 99, however it keeps reading until the max size (100) and maybe 1000 if I increase the maximum size it can read, it also shows some numbers after the last number 99, looks like this
constint ARRAY_SZ = 100 ; // upper limit on numbers that can be read
int array[ARRAY_SZ] = {0} ; // initialise to all zeroes
int cnt = 0 ; // count of numbers that were actually read
std::ifstream input_file( "input.txt" ) ; // open file for input
// read up to a maximum of ARRAY_SZ numbers from the file
while( cnt < ARRAY_SZ && input_file >> array[cnt] ) ++cnt ;
std::cout << cnt << "numbers were read\n" ;
// print out the numbers that were read
for( int i = 0 ; i < cnt ; ++i ) std::cout << array[i] << ' ' ;
std::cout << '\n' ;