Reading file values by passing reference to a function

Hi everyone!

I am trying to read the file which has the values stored in the following pattern

2 4 10 103 2 504 .... and so on

I Have opened the file and then passed the opened file to another function to process it further.

here is 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
36
37
#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h>

using namespace std;

void readingValues(std::ifstream& myFile);

int main()
{
    ifstream myFile;
    myFile.exceptions(ifstream::failbit | ifstream::badbit);
    try{
        myFile.open("integers.txt",ios::in);
        readingValues(myFile);
        myFile.close();
    }catch(std::ifstream::failure e){
        cout<<"Failed to open the file integers.txt!";
    }

    return 0;
}

void readingValues(std::ifstream& myFile){
    vector<int> dynamicArray;
    int myFileValues;
    
    while (myFile >> myFileValues) {
    	dynamicArray.push_back(myFileValues);
    }
	cout << "Values Stored In Array:\n";
	for (int i=0; i<dynamicArray.size(); i++) {
		cout << dynamicArray[i] << '\n';
	}
	cin.get();
}


now the problem is when the control exits the "while loop" in the function "readingValues(std::ifstream& myFile)it goes straight to catch block ??? any idea why? and how to fix it?

Regards.
I think the catch block executes at the end of file when the error flag is set. Try the following modification:
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
36
37
38
#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h>

using namespace std;

void readingValues(std::ifstream& myFile);

int main()
{
    ifstream myFile;
    myFile.exceptions(ifstream::failbit | ifstream::badbit);
    try{
        myFile.open("integers.txt",ios::in);
    }catch(std::ifstream::failure e){
        cout<<"Failed to open the file integers.txt!";
        return 1;
    }
    readingValues(myFile);
    myFile.close();

    return 0;
}

void readingValues(std::ifstream& myFile){
    vector<int> dynamicArray;
    int myFileValues;
    
    while (myFile >> myFileValues) {
    	dynamicArray.push_back(myFileValues);
    }
	cout << "Values Stored In Array:\n";
	for (int i=0; i<dynamicArray.size(); i++) {
		cout << dynamicArray[i] << '\n';
	}
	cin.get();
}
Last edited on
In line 13 you tell myFile to throw an exception on failure. And that's just what it does.
Get rid of line 13.
Hi Caligulaminus!

Thank you very much but if I remove line 13! then if the file is not there my program will crash, i am handling the exception?

Why would it crash?
you're trying to tell me exception handling is useless stuff? if the file is not there and i try to open the file so definitely things will go wrong so that's why i have to use exception handling...

Thank you very much but if I remove line 13! then if the file is not there my program will crash, i am handling the exception?


That's true. On the other hand, when the input extraction on line 39 fails an exception is going to be thrown. And since there aren't associated try-and-catch blocks in readingValues, that exception is not going to be caught in the function. The code on line 32 and below is currently unreachable.

Your code would be much simpler without exceptions.

Here's one way you could write it with exceptions enabled on stream operations:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

void readingValues(std::ifstream& myFile);

int main()
{
    ifstream myFile;
    myFile.exceptions(ifstream::failbit | ifstream::badbit);
    try{
        myFile.open("integers.txt", ios::in);
        readingValues(myFile);
        myFile.close();
    }
    catch (std::ifstream::failure e){
        cout << "Failed to open the file integers.txt!";
    }

    return 0;
}

void fillArray(std::istream& is, std::vector<int>& v)
{
    try
    {
        int value ;
        for (;;)
        {
            is >> value;
            v.push_back(value);
        }
    }

    catch (...)
    {
    }
}

void readingValues(std::ifstream& myFile){
    vector<int> dynamicArray;
    int myFileValues;

    fillArray(myFile, dynamicArray);

    cout << "Values Stored In Array:\n";
    for (int i = 0; i < dynamicArray.size(); i++) {
        cout << dynamicArray[i] << '\n';
    }
    cin.get();
}
Last edited on
[Edit: Double post due to some internet weirdness. Disregard this post.]
Last edited on
1
2
3
ifstream myFile("integers.txt");
if(myFile.is_open())
{...}


No exceptions needed.
Thanks All of you especially cire and Caligulaminus :)

cheers
Topic archived. No new replies allowed.