How to Search for a word in c++
    I suggest searching for words in a text file using the C ++ programming language, but before looking at code and video, you all need to understand its key points before getting started.Coding for these words
   C++ is a powerful general-purpose programming language. It can be used to develop operating systems, browsers, games, and so on. C++ supports different ways of programming like procedural, object-oriented, functional, and so on. This makes C++ powerful as well as flexible.
iostream
    Declares objects that control reading from and writing to the standard streams. This include is often the only header you need to do input and output from a C++ program.
Syntax
#include<iostream>Name | Descrotion |
---|---|
Cin | Specifies the Cin global stream. |
Cout | Specifies the Cout global stream. |
Cin
    Specifies the Cin global stream.
    Extern Istream cin
Return
    An istream Object.
Cout
    Specifies the Cout global stream.
    Extern Ostream Cout
Return
An ostream Object.
fstream
    This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files
Writing to a File
    While doing C++ programming, you write information to a file from your program using the stream insertion operator (<<) just as you use that operator to output information to the screen. The only difference is that you use an ofstream or fstream object instead of the cout object.
Reading from a File
    You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cinobject.
Read and Write Example
    Following is the C++ program, which opens a file in reading and writing mode. After writing information entered by the user to a file named Student.txt, the program reads information from the file and outputs it onto the screen
Example
How to Search for a Word in C++ Programming
How to Search for a word in a text file and if found print out the entire line.
ID | First Name | Last Name | Age | Phone Number |
---|---|---|---|---|
1 | Pov | Dara | 32 | 4444 |
2 | Heng | Nimol | 20 | 5555 |
Code
#include <iostream<
#include <fstream<
using namespace std;
class student{
int id,age,phone;
char first[20],last[20];
public: void writing()
{
cout<<"Please input Number ID";cin<<id;
cout<<"Please input first name";cin<<first;
cout<<"Please input last name";cin<<last;
cout<<"Please input age";cin<<age;
cout<<"Please input Phone Number";cin<<phone;
ofstream os;
os.open("student.txt",ios::app|ios::out);
os<<id<<"\t"<<first<<"\t"<<last<<"\t"<<age<<"\t"<<phone<<"\t"<<endl;
os.close();
}
void deletedata()
{
string line,id;
cout<<"Please input id do you to delete";
cin<<id;
ifstream writedata;
ofstream readdata;
writedata.open("student.txt");
readdata.open("temp.txt");
while(getline(writedata,line))
{
if(line!=id)
{
readdata <<line <<endl;
cout<<"Delete Succcess" <<id;
writedata.close();
readdata.close();
remove("student.txt");
rename("temp.txt","student.txt");
}
}
}
void searchfile(){
ifstream filesearch;
filesearch.open("student.txt");
string line, search;
cout << "Please input Phone Number do you want to search ";
cin >> search;
for (unsigned int i = 1; getline(filesearch, line); i++)
{
if (line.find(search) != string::npos)
{
cout << "found: " << search << " line: " << i<<endl;
}
else
{
cout << "Not found Data" << i << endl;
}}}};
int main()
{
student obj;
int i;
cout<<"Please Choose option is below"<<endl;
cout<<"1: Write Data"<<endl;
cout<<"2: Detele Data"<<endl;
cout<<"3: Search Data"<<endl;
cin>>i;
switch(i){
case 1: {
obj.writing();
break;
}
case 2:
{
obj.deletedata();
break;
}
case 3:
{
obj.searchfile();
break;
}}}
Please Watching My Video is Below
No comments:
Post a Comment