Hi,
I have a txt file with some details as given in the below format:
filename index file position file length

ex:

0001.ogg 123 000001 2345
0002.ogg 124 000099 3457
......
.....

I need to read the third parameter (i.e file position) with corresponds to its file name (first parameter).

Please help me with a sample code or algorithm which can search the string (file name ) and read its data (file position) with a good speed.
The requirement is that I have to search for 869 files.

Please help me as I do not know much about file handling in C++.

Here is a short ifstream tutorial. Its a little old, so replace fstream.h with fstream because current c++ standards have dropped the .h file extension.

> search the string (file name ) and read its data (file position) with a good speed. The requirement is that I have to search for 869 files.
the number of files are quite small; if you have to do this search more than once, it would be faster to read the file once and store the data in a lookup table in memory. the following example uses boost.tokenizer ( http://www.boost.org/ )

#include <iostream>
#include <fstream>
#include <boost/tokenizer.hpp>
#include <string>
#include <map>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace boost;

bool init_map( const char* const file_name, map< string, size_t >& np_map )
{
  ifstream file( file_name ) ;
  if( !file ) return false ;
  string line ;
  while( getline( file, line ) )
  {
    tokenizer<> toker(line);
    tokenizer<>::iterator iterator = toker.begin() ;
    string fn = *iterator++ ;
    np_map[fn] = lexical_cast<size_t>( *++iterator ) ;
  }
    return !np_map.empty() ;
}

int main()
{
  const char* const file_name = "whatever.txt" ;
  map< string, size_t > name_pos_map ;
  init_map( file_name, name_pos_map ) ;

  // look up
  string fn("0002.ogg") ;
  if( name_pos_map.find(fn) != name_pos_map.end() )
    cout << "found " << fn << " pos: " << name_pos_map[fn] << '\n' ;
  else
    cout << "did not find " << fn << '\n' ;
}

Dear Vijayan,
Thanks a lot for your kind help.
The thing is once I read the data (file position)after searching using file name I wanted to just store it in a variable or a struct so that I can make use of that file position to find its file buffer in another file. Is it possible without using boost lib. If you have any that kind of sample please provide me.
Once again Thanks a lot for the help.

here is a version without boost. note: the code is more brittle, eg. will break if the file format changes from what you have given to a simple csv.

#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <sstream>
using namespace std;
using namespace boost;

bool init_map( const char* const file_name, map< string, size_t >& np_map )
{
  ifstream file( file_name ) ;
  if( !file ) return false ;
  string line ;
  while( getline( file, line ) )
  {
     istringstream stm(line) ;
     string fn ; string x ; size_t pos ;
     if( stm >> fn >> x >> pos ) np_map[fn] = pos ; 
  }
    return !np_map.empty() ;
}

int main()
{
  const char* const file_name = "whatever.txt" ;
  map< string, size_t > name_pos_map ;
  init_map( file_name, name_pos_map ) ;

  // look up
  string fn("0002.ogg") ;
  if( name_pos_map.find(fn) != name_pos_map.end() )
    cout << "found " << fn << " pos: " << name_pos_map[fn] << '\n' ;
  else
    cout << "did not find " << fn << '\n' ;
}
This article has been dead for over six months. Start a new discussion instead.