954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

search in text file

Hi am trying to make a simple application that write and read from and to a text file the problem is that i want to be able to aerch in the text file for an specific line.

the text file is a list of words with a meaning each word, the application ask for what word to search and i want to cout<< the specific line containing that word..

could someone can help me ??

reaven
Junior Poster in Training
52 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

yes -- read each line sequentiall from star to finish until you get the line you want. use fstream's getline() to read an entire line then check only the first word to see if the line is the one you want.

Don't attempt to code all of that at one time. First get your program to open the file for reading. After that is code and compiles without error, add a loop using getline() to read each line. Get that working then code the next part.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

ok this is what i got

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	string line;
	char word;
	
	fstream dict("c://example.txt",ios::in);

	if (!dict.is_open()){ 
		cout << "Unable to open file";
		exit(1);
	}
	cout<<"Enter word to search \n";
	cin>>word;
	while(getline(dict,line)) 
		cout<<line<<"\n";
		

	return 0;
}

this is the part i know, now what ?


thanks in advanced !!

reaven
Junior Poster in Training
52 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

expand the while loop to extract the first word (hint: use string's find() method to locate the first space), then compare that with the string that you entered from the command line. If they are the same then you can stop that loop.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	string line;
	char* word="";
	size_t i;
	fstream dict("c://example.txt",ios::in);

	if (!dict.is_open()){ 
		cout << "Unable to open file \n";
		exit(1);
	}
	cout<<"Enter word to search \n";
	cin>>word;

	while(getline(dict,line))
        i = line.find(word);
       
        if (line.compare(word) != 0){
               cout << "result " <<line << endl;

      }else

		cout<<"Not in list"<<"\n";
		

	return 0;
}


I have the idea of what you are saying but dont know how to put it in code

reaven
Junior Poster in Training
52 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

A little off. First you need to put braces around that entire while statement. Next, the find mentiond returns string::npos if the word is not found. You need to add a check to see if the find method worked or not.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	string line;
	char* word="";
	size_t i;
	fstream dict("c://example.txt",ios::in);

	if (!dict.is_open()){ 
		cout << "Unable to open file \n";
		exit(1);
	}
	cout<<"Enter word to search \n";
	cin>>word;
	{
	while(getline(dict,line))   //read line by line
	i = line.find(word);    //try to find the var word in the string line an assign it to i
	
	if (i!=string::npos)      // dont exactly know what it does
  cout<<i<<" not found";      
	else 
		line.compare(word) != 0;  //compare the string line to the var word 
    cout << "result " <<line << endl;
	exit (1);
  
}

	return 0;
}

I know youre trying to make me understand but am a little lost here really !!
i put comments around the lines to see if i understand what each does correctly , please correct me if am wrong..

thanks a lot

reaven
Junior Poster in Training
52 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Why not try a trivial example first without reading a text file. When you are confident with how it works try it with a text file.
Questions (hints)
Are you getting your logic muddled up? Do you even need the line.compare(word)?
http://www.cplusplus.com/reference/string/string/find.html

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	string line;
	char* word="";
	size_t i;
	fstream dict("c://example.txt",ios::in);

	if (!dict.is_open()){ 
		cout << "Unable to open file \n";
		exit(1);
	}
	cout<<"Enter word to search \n";
	cin>>word;
	{
	while(getline(dict,line))   //read line by line
	i = line.find(word);    //try to find the var word in the string line an assign it to i
	
	if (i!=string::npos)      // dont exactly know what it does
  cout<<i<<" not found";      
	else 
		line.compare(word) != 0;  //compare the string line to the var word 
    cout << "result " <<line << endl;
	exit (1);
  
}

	return 0;
}

I know youre trying to make me understand but am a little lost here really !! i put comments around the lines to see if i understand what each does correctly , please correct me if am wrong..

thanks a lot

you put the braces in the wrong place. This is how it should look:

while(getline(dict,line))   //read line by line
{ // start of while

    // your code goes here

} // end of while
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I put it there but still dont work !!

it is my code right or wrong?

reaven
Junior Poster in Training
52 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

re-post your most recent code. What you posted earlier was not right, as I mentioned before. Pay close attention to the placement of the braces. compilers do not accept sloppy coding.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
using namespace std;

int main()
{
	
	string line;
	char word[65];
	size_t found=' ';

	fstream dict("c://dict.txt",ios::in);

	if (!dict.is_open()){ 
		cout << "Unable to open file \n";
		exit(1);
	}
	cout<<"Welcome \n"
		<<"Cois 270 \n"
		<<"\n"
		<<"\n"<<endl;

	cout<<"Enter a word to search: ";
	cin>>word;

	while(getline(dict,line)){
		if ( (found = line.find (word, 0)) != string::npos )

			cout<<"\n"<<line<<endl;				
	}
			
	if ( (found != line.find (word, 0)) == string::npos )
	cout<<"'"<<word<<"'"<<" is not found \n";

dict.close();
return 0;

}

somehow or someway i get it to work, dont know if is the right way to do it but it work...

I limit how much is display in the result by

char word[65]="";

if i let the char word; alone it display the entire text file starting from the word i was searching.

now am trying to convert the char word to lowercase in case someone type a word in upper
I try to use cast and tolower() but cast dont work and tolower() can convert int to char[65]...


Thanks a lot " Ancient Dragon " this is the best way to learn !!

reaven
Junior Poster in Training
52 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

that while loop is close to being ok, but not quite. If the word is found you need to jump out of that loop if you don't then the if statement that is after the loop will not work correctly.

That loop is also still incorrect because the return value of find could be somewhere in the middle of the string, which is not what you want. For example, lets say the word you want to find is "Mary" and the sentense is "Hello my name is Mary.". In this example the find method will return a value of 17 because "Mary" starts in position 17 of the string. But you want a string with the word "Mary" at the beginning of the string and ignore other places. This means that if statement has to be changed to check if found is 0, not npos.

while(getline(dict,line)){
    if ( (found = line.find (word, 0)) == 0 )
    {                                      
          cout<<"\n"<<line<<endl;	
          break;
     }
}


to convert to lower-case you need to call tolower() in a loop and call it for every letter in the word. There is another way, assumingword is std::string object and not a character array.

translate(word.begin(), word.end(), word.begin(), tolower);

You will also have to do the same thing with each line read from the file, just to insure that they are all the same case.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
using namespace std;

int main()
{
	
	string line;
	char word[65]="";
	char ans=' ';
	size_t found=' ';

	fstream dict("c://dict.txt",ios::in);

	if (!dict.is_open()){ 
		cout << "Unable to open file \n";
		exit(1);
	}
	cout<<"Welcomey\n"
		<<"Cois 270\n"
		<<"\n"
		<<"\n"<<endl;
	while(toupper(ans)!='X'){
	cout<<"Enter a word to search: ";
	cin>>word;

	while(getline(dict,line)){
		if ( (found = line.find (word, 0)) != string::npos )

			cout<<"\n"<<line<<endl;				
	}
			
	if ( (found != line.find (word, 0)) == string::npos ){
	cout<<"'"<<word<<"'"<<" is not found \n";
	}
		cout<<"Press 'X' to exit or any other key to continue searching ";
		cin>>ans;
}
dict.close();
return 0;

}


for now i add a loop to see if you want to search more words or not, but now the if statement that check if the word is not found is not validating and maybe is the braces :(

thanks

reaven
Junior Poster in Training
52 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

you obviously ignored my previous post so I'll ignore yours.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

i change to ==0 but when i type a word that i know is in the text file at the beginning of the line it validate for !=0
BTW i check different words all validate by !=0 and always display the same line from the text file

reaven
Junior Poster in Training
52 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 
you obviously ignored my previous post so I'll ignore yours.

what ???

reaven
Junior Poster in Training
52 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

anyway the line that display is the first line in the text file...

reaven
Junior Poster in Training
52 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

well, when you get around to posting something that includes the fix that I last mentioned I might get around to answering you. In any event -- to late for me tonight, going to sleep. Good night. :)

AD

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I already did what you said
i change to ==0 but when i type a word that i know is in the text file at the beginning of the line it validate for !=0
BTW i check different words all validate by !=0 and always display the same line from the text file(the line is the first one from the text file)


i post the other code because i didn't receive an answer so i move on to other thing when i was typing that you were typing your post ...

reaven
Junior Poster in Training
52 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You