Input file will not open

I cannot for the life of me figure out why every time I run this my input file fails to open. I have the weatherData.txt full of numbers I plan on setting to variables and plugging into equations but I just can't get the file to open.

Note that I'm not getting a failed error for my output file. I don't have a test.txt because I assume I never output anything to it so that is ok correct?

Thank you in advance for any help!
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
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

void open_files(ifstream& in_stream, ofstream& out_stream);

int main()
{
    ifstream in_stream;
    ofstream out_stream;

    in_stream.open("weatherData.txt");
    out_stream.open("test.txt");

    open_files(in_stream, out_stream);

    in_stream.close();
    out_stream.close();

    return 0;
}

void open_files(ifstream& in_stream, ofstream& out_stream)
{
    in_stream.open("weatherData.txt");
    if(in_stream.fail())
    {
        cout << "Input file opening failed.\n";
        exit(1);

    }

    out_stream.open("test.txt");
    if(out_stream.fail())
    {
        cout << "Output file opening failed.\n";
        exit(1);

    }
}
Last edited on
The open is failing in open_files because you've already opened it in main. In the listing above, all you need to do is remove lines 14 and 15.
Very true I don't know why I didn't catch that considering my function name is literally open_files. Unfortunately though I'm still unable to open this input file with those changes
In that case it's probably not in the right place. Are you using an IDE? If so, which one?
Ah I figured out my pathing was bad! Manually wrote it out now and the error stopped. Thank you though for pointing out I was opening it twice!
Topic archived. No new replies allowed.