I'm building a code that must search for a line of text in a string variable that contains many lines of text (example the text variable has lines formed like this MILAN;F205).
Once the user has entered the city he wants to search for, the program must search the database for the city entered by the user (in this case Milan) and extract only that line so in this case it extracts MILAN;F205 and puts it in the variable result.
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <algorithm>
using namespace std;
string file_content();
int main()
{
string database = file_content();
string result,city;
int len_database = database.size();
cout<<"which city do you want to look for? ";
cin>>city;
//cerchiamo la città nel database
if (database.find(city) != string::npos)
{
//cout<<"the code is present in the database"
//how should i continue?
}else{
cout << "it is not present in the database! ";
return 0;
}
cout << result;
return 0;
}
string file_content()
{
// filestream variables
fstream file;
string filename;
// we declare filename which corresponds to the file we want to open
filename = "lista-codici.txt";
//open the file
file.open(filename.c_str());
// create a stringstream
stringstream buffer;
//passes the file buffer to the stringstream
buffer << file.rdbuf();
//reading the file
return buffer.str();
}
How do I extract a specific line of text present in the database variable?