Here’s how i would do it if i was you. Do not use iostream**.h** - their obsolete! Only use .h if there’s no way to avoid it, it’s C headers, not C++ headers!
#include <iostream>
#include <fstream>
#include <sstream>
#include <list>
using namespace std;
// Structure for containing data of multiple datatypes
struct Person {
string Name;
int Age;
};
// Needed to signal an error
struct PersonsException {};
class Persons {
private:
string m_Filename; // Contains filename
ifstream m_FileStream; // Contains file
list<Person> m_List; // List for storage
public:
Persons(string Filename) { m_Filename = Filename; }
int readFile();
void displayList();
};
int Persons::readFile() {
// Open file in filestream
m_FileStream.open(m_Filename.c_str());
// If no file open, throw an exception - function is terminated
if (!m_FileStream.is_open()) throw PersonsException();
// Read lines until end of file is reached
while (!m_FileStream.eof()) {
string Line;
// Read line from file and put into string
getline(m_FileStream, Line);
// If string is empty, ignore it
if (Line.empty()) continue;
// Put string into input stringstream
istringstream Stream(Line);
// Create a structure object to use for storing the data
Person Entry;
// Extract data from istringstream and put into structure
Stream >> Entry.Name >> Entry.Age;
// Add structure to list
m_List.push_back(Entry);
}
// Return size of the list
return m_List.size();
}
void Persons::displayList() {
// Ready iterator for list
list<Person>::iterator i;
// Iterate through the list
for ( i = m_List.begin(); i != m_List.end(); ++i) {
// Print each entry of the list
cout << i->Name << i->Age << endl;
}
}
int main(int argc, char **argv) {
// Try the class out
try {
Persons obj1("senarai.txt");
obj1.readFile();
obj1.displayList();
}
// Oops, an error was thrown
catch (PersonsException &e) {
cout << "An error occured - file may not exist." << endl;
}
system("pause");
return 0;
}
I have discarded all your C code, and replaced it with C++ code. You shouldn’t be using char arrays, fscanf and whatnot, it’s C functionality. The C++ functionality offers you many more features, and its guaranteed to work with other C++ functionality.
I’m using a structure to contain the name and age, and then store those structures inside a STL list. It’s better to use STL containers than your own arrays, as they are dynamically reallocated when data is added, thereby being unable to overflow. They also have a lot of functionality, and is guaranteed to work.
Another thing - do not use large functions as inline in classes, its unwise. The general rule is to only have very fast and small code inline, and all other code defined outside the class. It does matter, although i cannot give you a throughout explanation. You can read about it here: http://en.wikipedia.org/wiki/Virtual_table
By using C++ methods and STL containers, you’ll end up with an easier class - its unwise for a class to return its size, and to print out from that size. What if the size is larger than your array? It would cause an overflow.
Happy coding
I suggest you read up on C++ methods and STL containers, they’re really helpful. http://www.cplusplus.com/ is a great resource for all everything (standard) C++.
EDIT:
The textfile is formatted like this for my code: (I don’t know if yours was intended as such)
Peter 47
Ervin 36