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.
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.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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;
}
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