August 1st, 2002, 9:04 am
If at all possible use C++ as it comes with STL ( Standard Template Library ). If this is possible you can do something like:#include <stdio.h>#include <string>using namespace std;typedef struct{ string strName; int nAge; float fpSalary;} NameAgeSalary;vector< NameAgeSalary > vNAS;FILE *pF = fopen( "data.txt", "r" );if ( pF ){ while ( fgets( sz, sizeof( sz ) - 1, pF ) ) { char szName[1024]; int nAge = 0; float fpSalary =0; int n = sscanf( sz, "%[A-Za-z .] %[0-9] %[0-9]", szName, &nAge, &fpSalary ); if ( n == 3 ) { NameAgeSalary nas; nas.strName = szName; nas.nAge = nAge; nas.fpSalary = fpSalary; vNAS.push_back( nas ); } } fclose( pF );}You might want to use a map<> or something else instead of a vector as you will undoubtely want to do some indexing, etc. You might want to use iostreams instead of fgets and sscanf but only if you want your app to run really slow or if there isn't much data. You can probably find lots of code for this on the web. With the STL you don't have to worry about the memory allocation as STL takes care of it. Then you can pass a reference or a pointer to your vector ( or a copy if you want to make things really slow ) when you call functions, etc. You could also do the following if you really wanted to do the memory allocation yourself:vector< NameAgeSalary *> vNAS; NameAgeSalary *pnas = new NameAgeSalary; // or ( NameAgeSalary *)malloc( sizeof( NameAgeSalary ) );, this won't work as long as NameAgeSalary.strName is a string if ( pnas ) { pnas->strName = szName; pnas->nAge = nAge; pnas->fpSalary = fpSalary; vNAS.push_back( pnas ); }The variations are endless. Good luck.