Program isn't infile-ing the my infile.

Nov 20, 2020 at 2:53am
So, the problem I'm having is that my functions either aren't opening the file to drag out the numbers or just isn't pulling the numbers at all. I adjusted a shortened version to see if it was a problem with arrays or functions and even in the simplified form it isn't working. I need some 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
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

void openFiles(ifstream& infile, ofstream& outfile);
void getInt(ifstream& infile, ofstream& outfile, int& N);


int main()
{
	static ofstream outfile;
	static ifstream infile;

	int N = 0;
	openFiles(infile, outfile);

	getInt(infile, outfile, N);

	infile.close();
	outfile.close();

}

void openFiles(ifstream& infile, ofstream& outfile)
{
	infile.open("HeartAttackScores.dat");
	outfile.open("Test.txt");
}

void getInt(ifstream& infile, ofstream& outfile, int& N)
{
	infile >> N;
	outfile << N;

}



My input file reads as follows:
20
0034 14
1123 5
...

However, I'm just trying to pull the first integer, 20.
Nov 20, 2020 at 2:56am
To confirm that the infile is just not opening successfully:
1
2
3
4
5
infile.open("HeartAttackScores.dat");
if (!infile)
{
    cout << "Error: Could not open file\n";
}


How are you executing your program? The working directory of your program needs to be the same directory as where the file is.

Where is your Test.txt being written to when the program runs?

PS: (1) You don't need to explicitly call <file stream>.close(), it will be done for you when the files are destructed. (2) There's no point in making your outfile/infile variables be static since they're in main anyway.
Last edited on Nov 20, 2020 at 2:59am
Nov 20, 2020 at 3:25am
I realize now that I am in fact unintelligent. I appreciate you, Ganado. The files were not where they were supposed to be.
Nov 20, 2020 at 4:05am
Don't worry, it's a mistake a lot of people make, especially when using an additional layer like that of an IDE which makes it harder to tell where the program is executing from.
Topic archived. No new replies allowed.