Stop reading numbers from a file

Jan 26, 2017 at 11:06pm


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++;
	}
Jan 26, 2017 at 11:16pm
Can you show more context?
Jan 26, 2017 at 11:27pm
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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]<<" ";
	
	}
	
}
Jan 27, 2017 at 2:18am
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <vector>

int main()
{
    std::vector<int> numbers{};
    std::ifstream inFile("D:\\input1.txt");
    int temp{};
    while (inFile >> temp)
    {
        if(inFile)
        {
            numbers.push_back(temp);
        }
    }
    for (auto& elem : numbers)
    {
        std::cout << elem << " ";
    }
    std::cout << '\n';
}
Jan 27, 2017 at 2:23am
I used vector, it worked perfect:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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.
Last edited on Jan 27, 2017 at 2:26am
Jan 27, 2017 at 2:40am
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.
Jan 27, 2017 at 2:48am
The numbers in .txt looks like this:
3
54
34
56
67
78
45
3
45
78
90
45
23
98
99


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
 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 0 0 0 0 0 0 0 0 0 0 0 3.35673e-23 6.95321e-310 6.95321e-310 6.95321e-310 6.95321e-310 4.94066e-324 6.95321e-310 2.24746e-314 6.95321e-310 6.9532
Jan 27, 2017 at 3:06am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const int 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' ;
Jan 27, 2017 at 3:24am
It works! Thank you JLBorges!
Topic archived. No new replies allowed.