Hi all,
This is quite a specific question with a relatively large amount of explaining so ill try to make it really short to try save peoples time.
In a nutshell what i have is a large csv file and im trying to search a particular entry (line in the file) as fast as possible.
I origianlly just read the whole file and kept all the elements in an array of struct in memory but it has grown massively large, so that is not an option anymore. The file contains the collection of structures with each element of the structure seperated by a comma and each structure on a new line. so something like this
elementId,int,float,string,int,int,<lots more>
example
0,463,5.67,tom,marketing,1,0,....
1,365,5.78,john,sales,4,1,...
What im currently doing is something like this ( not real code so it might have typo's / syntax errors sorry )
void CSVManager::FetchEntryFromCSV( uint32_t nId, struct csvElement_st& result )
{
/* string to store the line from the text file */
string sFileEntry;
/* open file handle */
ifstream fCSVFile;
fCSVFile.open ( m_sCsvFilePath, ifstream::in );
/* skip the records i dont want */
for( uint32_t n = 0; n < nId; n++ )
{
getline( fCSVFile, sFileEntry );
}
/* The previous loop skipped all the records that come before the one i want
* so the next line in the file is the element i want
* so get this one from the file
*/
/* get the record i do want */
getline( fCSVFile, sFileEntry );
/* split out all the comma's and put the actual elements into the struct */
ParseLineToStruct( sFileEntry, result );
/* the struct passed in now has the details from the csv file so were done here :) */
}
And now finally the core of the question is,
Can i make this faster and ideally as close to constant time to look up any element?
I had considered working from the start or end of the file based on if the index was over 50% of the file down ( i know this information up front ) but i dont know how to do a reverse file handle that would do getline() from the bottom up.
Is there any quick way i can do a goto line without seeking through the whole file or is that asking too much?
Many thanks in advance and if anything is not clear just ask ill try to explain it better.
Thanks Kano