Saving / opening / saving arrays

Jul 23, 2018 at 8:10am
Is this the best way to go about saving / opening / saving an array.

had the first program because the file pathway to storing a file was a problem, seems solved.

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 <iomanip>
#include <fstream>

using namespace std;

const int TOTALYEARS = 100;
int main()
{


	int ageFrequency[TOTALYEARS]; //reserves memory for 100 ints
	int currentAge;

	for (int count = 0; count < TOTALYEARS; count++)
		ageFrequency[count] = 0;

	fstream inputFile;
	fstream outputFile;

	inputFile.open("C:\\Users\\VISUALBASIC PROJECT FOLDER\\insertAgesFromKeyboardArray\\Employees.txt");
	int number = 0;

	while (number < TOTALYEARS && inputFile >> ageFrequency[number])
	{
		number++;
	}
	inputFile.close();
	
	cout << "Please input an age from one to 100. Input -99 to Stop" << endl;
	cin >> currentAge;

	while (currentAge != -99)
	{
		ageFrequency[--currentAge] += 1;
		cout << "Please input an age from one to 100. Input -99 to Stop" << endl;
		cin >> currentAge;
	}
	outputFile.open("C:\\Users\\VISUALBASIC PROJECT FOLDER\\insertAgesFromKeyboardArray\\Employees.txt");
	for (int count = 0; count < TOTALYEARS; count++)
	{
		outputFile << ageFrequency[count] << endl;
	}
	outputFile.close();
	for (int count = 0; count < TOTALYEARS; count++)
		{
			cout << ageFrequency[count] << " " << endl;
		}


	cin.get();
	return 0;
}
Jul 23, 2018 at 8:38am
inputFile.open("C:\\Users\\VISUALBASIC PROJECT FOLDER\\insertAgesFromKeyboardArray\\Employees.txt");

Do yourself a favour; just use forward slash for directory separator:
inputFile.open("C:/Users/VISUALBASIC PROJECT FOLDER/insertAgesFromKeyboardArray/Employees.txt");
Jul 23, 2018 at 8:56am
You could also write on a binary file. I think it would be less tedious for you.
Topic archived. No new replies allowed.