Building array of objects

I'm a little new at this, forgive me if I seem obtuse.

I've built a Pollution class. It looks like this.
1
2
3
4
5
6
7
8
class Pollution
{
	private:
		int hour, co2, h2s, suspended_particles;
	public:
		void read_data();
		void print_data();
};


I've called an array of these objects like this
Pollution record[20]; // record is an array of 20 Pollution objects

The read_data() function isn't working. It looks like this.
1
2
3
4
5
void Pollution::read_data()
{
	ifstream infile("data.dat");	
	infile >> co2 >> h2s >> suspended_particles;
}


It works fine except that for each Pollution object in the array, a new infile object is created, so that the first three values in the file are used for every object in the array. I'm using a loop to populate all of the data in all of the Pollution objects. it looks like this.
1
2
3
4
for (int i=0; i<20; i++)
	{
		record[i].read_data();
	}


Can I either pass an infile object to the read_data() method so that it stays in scope, or pass an entire array (or address to the array) to a member function of it's own class? The read_data() function must be a member of the Pollution class.

Thanks in advance, I appreciate whatever assistance can be afforded.
Pass in the ifstream by non-const reference and that will fix the problem.
I found this to be an interesting question so I did a bit of looking around and found a solution I think would work. I've still got a long ways to go myself so I don't know if everything I'm doing is good practice, so take it with a grain of salt. That being said, I tested this code and it works fine for me.

Because you're using ifstream objects you have access to tellg and seekg, which are the getter/setter functions for the get pointer in your ifstream object. Based on that, I created a function that would return the location in the file that it ended at. I can use that the next time I call it to ensure that I start at the same place. Because -1 isn't a viable location I return that if I encounter an error or the end of file. If you were to do it like this you could just call read_data on a Pollution object, store the location result, and send it to the next object in the array.

Here is my test code. I hope this helps:
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
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int readWordFromFile(const string& testString, int location = ios::beg)
{
	// Make sure that the location is actually within the file.
	if (location < 0)
	{
		return -1;
	}

	// Open the file and ensure that it worked.
	ifstream fin(testString.c_str());
	if (!fin.is_open())
	{
		return -1;
	}

	// Go to the location sent in if it isn't past the end.
	fin.seekg(0, ios::end);
	if (location < fin.tellg())
	{
		fin.seekg(location);
	}
	else
	{
		return -1;
	}

	// Get the next value, and for the purposes of debugging, display it.
	string returnWord;
	fin >> returnWord;
	cout << returnWord << endl;

	if (fin.eof())
	{
		// Let the user know that this location is no good.
		fin.close();
		return -1;
	}
	else
	{
		// Return the current location to the user.
		int endingLocation = fin.tellg();
		fin.close();
		return endingLocation;
	}
}

int main()
{
	int continueLooping = ios::beg;

	string fileName = "Test.txt";

	while (continueLooping != -1)
	{
		// Set continueLooping to the new result.
		continueLooping = readWordFromFile(fileName, continueLooping);
	}

	cout << "Press enter to continue . . .";
	cin.get();
	
	return 0;
}

But why pass in a file offset instead of the stream object itself?

Well, you could definitely pass in a reference to the object and that would work too, especially if you were going to initialize all the members of the array at the same time every time. I think I'm a bit overtired and overcomplicated it. >.>

Basically, I can see that method being good if you wanted the object to be able to open up the file and get the next bit of information at different times in the program. Like if you didn't need to get all of the data at the same time.

Looking at it now though, I would probably use the non-const reference too....I think I need to get some more sleep. :P
Topic archived. No new replies allowed.