Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading specific text from a .txt file.

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
hey,

i hope you can understand this.

I was just curious if it is possible to take a .txt document and pick out specific words or phrases

example.

//the text is

hello people, i are stupid
you is not stupid
i am happy
cows are funny
i have 1st grade education.

i want to be able to type in "cows" and get "cows are funny"
to print out. or i imput "people" and get "people, i are stupid"

i might be going about this all wrong, but i would appreciate it if you could give me a hand.

thanks much in advance...

 
Hi

Assume a dialog box with the following controls:
CEdit Id = IDC_TEXT (Multi-Line)
CEdit Id = IDC_TYPE where you type in
CStatic Id = IDC_LINE_FOUND where the line found is displayed

Fill the Multi-Line Edit box with the content of the file.

Use the following code to get the word type in:

CString str;
CString strLine;
char szBuffer[200];

if ( !GetDlgItemText( IDC_TYPE, str))
return;

The word you are looking for is in the variable str.

Use the following code to get the line that contains the word stored in str:

CEdit* pEdit = ( CEdit*) GetDlgItem( IDC_TEXT);

int nLineCount = pEdit->GetLineCount();

for ( int nLine = 0; nLine < nLineCount; nLine++)
{
memset( szBuffer, '\0', 200);

pEdit->GetLine( nLine, szBuffer, 200);

if ( strstr( szBuffer, str) != NULL)
SetDlgItemText( IDC_LINE_FOUND, szBuffer);
}

Note that this code does not try to verify if there is several lines with the word you type in. This is an additional work left to do ...

HTH

Thierry
EMail: Thierry.Marneffe@swing.be
WebSite:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top