#include <string>
#include <sstream>
#include <iostream>
int main()
{
// open the stream which contains the city data
// (this example uses a stringstream instead of a filestream)
std::istringstream stm( R"(
A|City1|Data.1|Data.2|Data.3
R|01R|Data.4|Data.5|Data.6
R|02R|Data.4|Data.5|Data.6
R|03R|Data.4|Data.5|Data.6
R|04R|Data.4|Data.5|Data.6
A|City2|Data..1|Data..2|Data..3
R|01R|Data..4|Data..5|Data..6
R|02R|Data..4|Data..5|Data..6
R|03R|Data..4|Data..5|Data..6
R|04R|Data..4|Data..5|Data..6
A|City3|Data...1|Data...2|Data...3
R|01R|Data...4|Data...5|Data...6
R|02R|Data...4|Data...5|Data...6
R|03R|Data...4|Data...5|Data...6
R|04R|Data...4|Data...5|Data...6
)" ) ;
// get the city name
std::string city_name = "City2" ;
// we need to look a line starting with
// the city_name prefixed by "A|" and suffixed by '|'
const std::string search_string = "A|" + city_name + '|' ;
// read and discard lines from the stream
// till we get to a line starting with the search_string
std::string line ;
while( std::getline( stm, line ) && line.find(search_string) != 0 ) ;
// check if we have found such a line, if not report an error
if( line.find(search_string) != 0 )
{
std::cerr << "city '" << city_name << " was not found\n" ;
return 1 ;
}
// we need to form a string that would include the whole set of data
// based on the selection of City
std::string result = line + '\n' ; // result initially contains the first line
// now keep reading line by line till we get an empty line or eof
while( std::getline( stm, line ) && !line.empty() )
result += line + '\n' ; // append this line to the result
// use the result (in this example, we just print it out)
std::cout << "data for '" << city_name << "'\n--------------\n" << result ;
}