In order to perform operations on your data, you need to load it in the memory in way it is convenient for you to use. If I were you, I would make a structure or class for that purpose - which can hold the data of one record. Then I would make an "array" of the structure, so I could store all the data from the file. I would use vector , or list from the stl , if you need to insert and remove elements frequently choose list, otherwise vectors are fine. Then read all the data from the file into the vector/list.
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
struct Record
{
std::string name, address;
int client_number;
/*
.
other stuff you need to store
.
*/
};
bool init ( std::vector<Record>& );
int main ()
{
std::vector<Record>clients;
if ( ! init ( clients ) ) return -1;
// if it fails to open the file the program returns with -1
// since you wouldn't have any data to work with, there is no point
// running the program any further, or you can check the size of the vector
/*
.
. other function callings
.
*/
return 0;
}
bool init ( std::vector<Record> &v )
{
Record tmp; // to store each record
std::ifstream in ( "TEST.txt" );
std::string line;
std::stringstream ss; // to manipulate the string
if ( !in ) return false;
while ( getline ( in, line ) )
{
// I dont know exactly how your file is arranged
// but there must be a deliminator character somewhere between the records.
// If not, count the lines you've read, and make a check each time you are
// reading a new record. You can ensure you are reading the right data
// by checking the line. - all records start with "ClientNum"
// and depending on who your file looks like, you can extract the string with stringstream
// ss << line;
// ss >> tmp.client_number;
// ss >> tmp.name;
// or you can use getline ( ss, tmp.name, '\n' ); dependig who you want to extract the stream
// etc.
v.push_back ( tmp ); // and finally push back the data into the vector
}
in.close ();
return true; // all went good, return true
}
After you have read all the data into the vector, you can work on it comfortably and implement all your functions.