Oct 25, 2011 at 12:26pm UTC
Hello, i am a beginner and need some help.
i need a search function for more than one word...
i dont know how to do this.
plz help.
########
char suche[512] = "findthis"; // here i need more words to search
char *pos;
pos = strstr(line, suche);
if(pos)
{
printf("Match at %d. in Array.", pos-line+1);
//cout << pos-line << endl;
string found;
found=line;
cout << found << endl;
}
else
{
printf("Nothing found!");
}
}
cout << endl;
################
complete code:
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <string.h>
using namespace std;
int main (void)
{
char line[256];
ifstream infile ("IKA_ANF.anf", ios::in);
if (!infile)
{
cout << "No File :-(" << endl;
return 1;
}
while (infile.getline (line, 256))
{
// do something
cout << line << endl;
char suche[512] = "Sven";
char *pos;
pos = strstr(line, suche);
if(pos)
{
printf("Match at %d. in Array.", pos-line+1);
//cout << pos-line << endl;
string found;
found=line;
cout << found << endl;
}
else
{
printf("Nothing found!");
}
}
cout << endl;
return 0;
}
Oct 25, 2011 at 4:31pm UTC
use std::string
instead of array. There isn't (normally) any benefit to use array. You can use then size_t find ( const string& str, size_t pos = 0 ) const ;
member function to search for the phrase you wish.