Hi all,

I have done a word search within a text file and managed to find it on a particular line,
but i dont know what to do so as to get the word/number directly after the searched word.

e.g content with the text file

sdakodjskfjsdkfjasook
sample1. : ihavemakeit
sdofjsdfjasfjsdkf
dflsdj

sample2

Any help will be greatly appreciated.

ifstream in("sample.txt");
if (in!=NULL)
	MessageBox::Show ("file exist");
		string line;
		std::string match("sample1\.   \:");

		while (getline(in, line))
		{
			stringstream sstrm(line);
			std::string::const_iterator iter =
			std::search (
			line.begin(),
			line.end(),
			match.begin(),
			match.end()
					);
								
		if (iter != line.end() )
		MessageBox::Show ("word found:");

		//from here what should i do get the 
		//whole line, if possible i just wanted to get
		//words/numbers after the "sample.    :"
		//e.g sample.   : ihavemakeit
		//for this case i just wanted to get this "ihavemakeit"
		//i have tried using convert string to char but 
		// dont seem to work.

You might use std::string find() to see if the line contains the search string.
And when it does, you can extract the rest of the line using std::string substr().

find()
substr()

The string class does have methods specifically for doing that sort of stuff.
But you can use the standard algorithms just as easily.

#include <algorithm>   // needed by using_iterators()
#include <cctype>      // needed by using_iterators()
#include <functional>  // needed by using_iterators()
#include <iostream>
#include <iterator>    // needed by using_iterators()
#include <string>
using namespace std;

//------------------------------------------------
void using_iterators( string s, string w )
  {
  string::iterator i = search( s.begin(), s.end(), w.begin(), w.end() );
  if (i == s.end())
    {
    cout << "iterators> The text did not contain the word '"
         << w << "'.\n";
    return;
    }

  i = find_if( i +w.length(), s.end(), not1( ptr_fun <int,int> ( isspace ) ) );
  if (i == s.end())
    {
    cout << "iterators> There is no text following the word '"
         << w << "'.\n";
    return;
    }

  string::iterator j = find_if( i, s.end(), ptr_fun <int,int> ( isspace ) );
  string found;
  copy( i, j, back_insert_iterator <string> ( found ) );

  cout << "iterators> The word following '" << w << "' is '"
         << found << "'.\n";
  }

//------------------------------------------------
void using_indices( string s, string w )
  {
  string::size_type i = s.find( w );
  if (i == string::npos)
    {
    cout << "indices>   The text did not contain the word '"
         << w << "'.\n";
    return;
    }

  i = s.find_first_not_of( " \t", i +w.length() );
  if (i == string::npos)
    {
    cout << "indices>   There is no text following the word '"
         << w << "'.\n";
    return;
    }

  string::size_type j = s.find_first_of( " \t", i );
  string found;
  found = s.substr( i, (j == string::npos) ? j : (j -i) );

  cout << "indices>   The word following '" << w << "' is '"
         << found << "'.\n";
  }

//------------------------------------------------
int main()
  {
  string s, w;

  cout << "Press ENTER without inputting any text to quit.\n\n";

  cout << "What is the word you wish to find?> ";
  getline( cin, w );
  if (w.empty()) return 0;

  while (true)
    {
    cout << "\nPlease enter some text containing the word '"
         << w << "'\n> ";
    getline( cin, s );
    if (s.empty()) break;

    using_iterators( s, w );
    using_indices(   s, w );
    }

  cout << "Good-bye.\n";
  return 0;
  }

Hope this helps.

Thanks Duoas,

I have included some parts of your code into mine but i would like to ask how can i get the textBox to display the "found" . Cos im using window form while cin/cout are not needed.

Thanks

if (iter != line.end() )
 MessageBox::Show ("word found:");
 string::iterator j = find_if( iter, line.end(), ptr_fun <int,int> ( isspace ) ); 
 string found;  
 copy( iter, j, back_insert_iterator <string> ( found ) );
 textBox1->Text=(found);

Sorry i cant find the edit button.

Another question is i cant seem to get into using_iterators () from the button function, how to go about doing it? error msg is error C3861: 'using_iterators': identifier not found

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{	
	 using_iterators ();
}

That was just an example function I created. I expected that you would take the code you needed from it and put it in your own function.

I don't think that TTextBox::Text takes a std::string, does it? You might have to say textBox1->Text = found.c_str(); Hope this helps.

commented: congrats on featured poster +6

That was just an example function I created. I expected that you would take the code you needed from it and put it in your own function.

I don't think that TTextBox::Text takes a std::string, does it? You might have to say textBox1->Text = found.c_str(); Hope this helps.

Thanks Duoas for your reply.

I have tried that before but it comes with this error, "error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : cannot convert parameter 1 from 'const char *' to 'System::String ^'".

BTW, im testing the code in my form1.h does this matter?

Now im able to show it in the textbox using another alternative which is as followed:

string::size_type j = line.find_first_of( " \t", iter );
	string found;
	found = line.substr( iter, (j == string::npos) ? j : (j -iter) );

	 const char * buffer=found.c_str();
	string str (found.c_str());
	 int sizeNum = str.length();
	 long size = sizeNum;\

	ofstream outfile ("temp.txt");
	outfile.write (buffer,size);
	outfile.close ();
								textBox2->Text = System::IO::File::ReadAllText("temp.txt");;
								clock++;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.