my question is simple ..
how to program to search for a word in a text file that already exists ans if that specific word is found anywhere in the text the entire line containing that word to be print on the screen.
While searching for a word in a string (sentence) bear in mind words might appear at beginning, middle or end of sentences with corresponding upper/lower case and punctuation issues. You haven't shared your .txt file, so it's difficult to say anything more specific but here is an approach using std::regex – read the file line by line into a string with getline() and for each line apply the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <string>
#include <regex>
int main()
{
std::string line{"It is a category!?"}; //read file line by line with getline() into this string
std::regex r{"\\b(catEgory)\\b", std::regex_constants::icase};
//search word (here actually 'category'), with word boundaries and isocase flag (case insensitive)
//basic intro to C++11 regex: http://www.cplusplus.com/reference/regex/ECMAScript/
//regex cheat-sheet: http://cpprocks.com/files/c++11-regex-cheatsheet.pdfif(std::regex_search(line, r))std::cout << line << "\n";
}
You were pretty close, however if you didn't find it on the first line you gave up.
Try this:
1 2 3 4 5 6 7 8 9 10 11 12 13
bool found = false;
while (getline(in, line))
{
if (line.find(look_for) != string::npos)
{
cout << " record found " << endl;
cout << line << "\n";
found = true;
break;
}
}
if (!found)
cout << "record not found ";
gunnerfunner,
I agree. It can lead to false positives, but this looks like an exercise to me so only a simple solution is required.
Sure the best would be to create a struct student, overload some operators and store it in a set or vector.
It already looks like you're searching the file for the registration number, so it looks like you only need to print the line when you find the number in the line, unless there is another problem.
A trick to avoid finding false positives. Look for the registration number at the beginning of the line, and followed by a tab:
...
look_for += '\t'; // search for the registration number followed by tab
...
if (line.find(look_for) == 0) {
cout << "Record found\n";
....
}
As for deleting a line, you'll have to:
1. open a temporary file.
2. go through your text file, copying all records to the temp file except for the one you want to delete.
3. Close the temp file and the input file.
4. Rename the temp file to be your input file.
#include <iostream>
#include <string>
#include <fstream>
#include <sstream> // for testing
// return true if the line starts with the registration number
// note: leading white space is ignored
bool has_reg_number( const std::string& line, int reg_number )
{
// http://en.cppreference.com/w/cpp/string/basic_string/stoltry { return std::stoi(line) == reg_number ; }
catch( const std::exception& ) { returnfalse ; }
}
std::ostream& copy_selected( std::istream& in, int reg_number, std::ostream& out = std::cout )
{
std::string line ;
while( std::getline( in, line ) )
if( has_reg_number( line, reg_number ) ) out << line << '\n' ;
return out ;
}
std::ostream& copy_not_selected( std::istream& in, int reg_number, std::ostream& out = std::cout )
{
std::string line ;
while( std::getline( in, line ) )
if( !has_reg_number( line, reg_number ) ) out << line << '\n' ;
return out ;
}
int main()
{
// use a string stream for testing
std::istringstream file ( "Reg# Name semester GPA\n""3955 kaleem khattAK 34 9\n""14435 shin shina 2nd 4\n""4545 khan khan 3rd 2\n""1443 not shin shina 2nd 4\n""3452 noor din 7th 2\n" );
// print matching records
copy_selected( file, 14435 ) ;
file.clear() ;
file.seekg(0) ;
std::cout << "---------------------------\n" ;
// delete records
{
std::ofstream new_file( "modified.txt" ) ;
copy_not_selected( file, 14435, new_file ) ;
}
// verify that the new file was created correctly
std::cout << std::ifstream( "modified.txt" ).rdbuf() ;
}
Unless the file contains many millions of lines, read the contents of the file into a container (vector), make all the additions/deletions/modifications to the items in the container, and finally write the contents of the container back into the file.