Opening a file

So I am having trouble writing a code that opens a file that I have saved on my computer. I need to access a file, open it, read it, and then close it.
this is what I have so far.

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
54
55
56
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <cmath>
using namespace std;

int main ()
{
	string Randomtxt;
	int value;
	int numbers;
	int even;
	int odd;
	double sum;
	double average;

	cout << "Butterfly Girl we need your help! What is the file name?" << endl;
	cin >> Randomtxt;

	ifstream inputFile(Randomtxt);
	if(Randomtxt)
	{
		cout << "Success!" << endl;

		while (Randomtxt >> value)
		{
			cout << value << endl;
			numbers++;
			sum += numbers;
			if(value % 2 ==0)
				even++;
			else
				odd++;
		}
	}
	else
	{
		cout << "There is an error opening the file." << endl;
	}
	if (numbers > 0)
		average = sum / numbers;
	else
		average = 0.0;

	cout << "There are " << numbers << "in the file" << endl;
	cout << "The even numbers are " << even << endl;
	cout << "The odd numbers are " << odd << endl;
	cout << "The sum is " << sum << endl;
	cout << "The average of all the numbers is " << average << endl;

	Randomtxt.close();

	return 0;
}
Last edited on
Make sure the file path is correct.
I dont know how to do that.
Start with placing the file in the same folder as the executable and running the executable from that folder (not from within your programming environment. Then if you type the name of the file correct, it should find it.

If you want to run your program from within your programming environment, place a copy of the file in the folder that contains your project file.

When in doubt, place the folder in a root directory, like C:\filename.txt.

When you enter the file name you should enter the full name, like "C:\filename.txt", if the file is not in the same folder.
On windows you may also have to use escape characters, so your would type "C:\filename.txt" like "C:\\filename.txt"

p.s. For your future posts, please place your code inside the code-tags (use the "<>"-button)
Last edited on
if(Randomtxt)
You're testing the filename, not the inputFile.

while (Randomtxt >> value)
You're trying to read from the string, not inputFile.

Randomtxt.close();
You're trying to close the string, not inputFile.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


Last edited on
ok how do I test the inputFile then?
Ok so I edited it and there are no longer any red marks on the side however it always comes up as there was an error opening the file.

any ideas?

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
54
55
56
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <cmath>
using namespace std;

int main ()
{
	string Randomtxt;
	int value;
	int numbers;
	int even;
	int odd;
	double sum;
	double average;

	cout << "Butterfly Girl we need your help! What is the file name?" << endl;
	cin >> Randomtxt;

	std::ifstream ifs ("Randomtxt", std::ifstream::in);
	if(ifs.is_open())
	{
		cout << "Success!" << endl;

		while (ifs >> value)
		{
			cout << value << endl;
			numbers++;
			sum += numbers;
			if(value % 2 ==0)
				even++;
			else
				odd++;
		}
	}
	else
	{
		cout << "There is an error opening the file." << endl;
	}
	if (numbers > 0)
		average = sum / numbers;
	else
		average = 0.0;

	cout << "There are " << numbers << "in the file" << endl;
	cout << "The even numbers are " << even << endl;
	cout << "The odd numbers are " << odd << endl;
	cout << "The sum is " << sum << endl;
	cout << "The average of all the numbers is " << average << endl;

	ifs.close();

	return 0;
}



Is your file called Randomtxt or Random.txt?
Randomtxt
Topic archived. No new replies allowed.