473,854 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Search a word from text file in C++

6 New Member
Im a beginner of C++. I want to solve this problem.
I have to search few key words from few text files. How to create a file and do the operation as mentioned .
Sep 21 '06 #1
7 63280
sin
13 New Member
system("grep text file");

=)

Have you even bothered to start writing that program ?
Sep 21 '06 #2
babysnoopy
6 New Member
system("grep text file");

=)

Have you even bothered to start writing that program ?

Im now im just trying to create the file and to display wat is in the text file.
But the main thing i need is to search key words in the document.
Sep 21 '06 #3
babysnoopy
6 New Member
i hav manage to create a file and read it..
the << system("grep text file");>> is it just add in like this?

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
// Create a file and write the inputs into the file.
// If the file is already exist, then write the data at the end of the file so that the
// existing data is not overwriten.
// Save the file before exit.
ofstream myFile;
myFile.open("Ci tyFile.txt",ios ::app|ios::out) ;
myFile<<"This file contains the cities name."<<endl<<e ndl;
myFile<<"Singap ore"<<endl;
myFile<<"Bangko k"<<endl;
myFile<<"Phnom Penh"<<endl;
myFile.close();

// Open the file which is saved and display the data in the file.
string line;

ifstream Myfile("cityFil e.txt");
if(Myfile.is_op en())
{
while(!Myfile.e of())
{
getline(Myfile, line);
cout<<line<<end l;
}
Myfile.close();
}
else
cout<<"Unable to open this file."<<endl;


system("grep text file");

system("Pause") ;
return 0;
}
Sep 21 '06 #4
sin
13 New Member
=)
no, that system() was a joke =)

you can use
line.find(patte rn,0);
if it's result != string::npos, your string is found
Sep 21 '06 #5
babysnoopy
6 New Member
Thanks for the idea...
But i dont really understand ur code.
Wat is the "pattern" stands for? And the number "0" there is equals to wat?
Finaly, u did mention "!= string::npos", how should i use it?
Im very sorry, im just a beginner...so i might need a kind of step by step...
Thanks sin.
Sep 21 '06 #6
babysnoopy
6 New Member
Forgot to include this part!

For example, in the text file i got "I like to eat Mc Donalds."
Therefore, i want to look the key word "eat" whether is it in the text file, if yes then it will display the word once.
Sep 21 '06 #7
sin
13 New Member
The code would be:

Expand|Select|Wrap|Line Numbers
  1. ....
  2. char*    search = "eat"; // search pattern
  3. int    offset; // where it was found (or not (-1))
  4.  
  5. // arg 2 (0) specifies the offset from where to begin seach
  6. if ((offset = line.find(search, 0)) != string::npos) {
  7.     cout << "found '" << search << "' @ offset " << offset << endl;
  8. }
  9. .....
  10.  
Sep 25 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1533
by: Josh | last post by:
I would like to search a text file on my server and then alert me on if the current date exist in that file. Any one have any code that searches text files. thanks in advance
4
2212
by: suzy | last post by:
hello, i want to search a text file for an occurrence of a string and replace some text after the match, then save the file. eg: if the file is: <start> Hello my name is tony, I am 22. Hello, my name is sonia, I am 18.
1
4407
by: Andrew | last post by:
Hi, im trying to create a small function which can create a binary tree from the entries in a text file and after that we can perform all the usual operations like search, insert and delete etc. Now i have entries something like this IPaddress Phone No 192.168.2.1 2223447689 192.168.2.2 3334449898 192.168.2.3 5667869089
1
1562
by: Prabhat | last post by:
Hello friends, I have one TEXT file that I use to log download information from website. The file will have unique Login ID for the downloads. When the user will come for next time for download the LOG entry should go under the same section (same ID number) so for this I need to search the text file for the ID and if the ID is found the program should append the NEW LOG entry to the end of Section. (section mark can be a line of ***** )...
3
1518
by: mse07 | last post by:
hi all i search about statement in text file by this code : LineFlag = LineFlag + 1 Line Input #1, strLine If LineFlag = 15 Then If InStr(1, strLine, " Transfer completed successfully.") <> 0 Then MsgBox "successful"
8
2356
by: ImanMan | last post by:
hi Am using the following code to search a textfile.The returned value will always be found in this case, is there any problem in the code?? Dim list As System.Collections.ObjectModel.ReadOnlyCollection _ (Of String) list = My.Computer.FileSystem.FindInFiles("C:\Find", _ Search.Text, True, FileIO.SearchOption.SearchTopLevelOnly) Dim i As Integer = 0
1
1205
by: ahmed222too | last post by:
i use vb6 i want to make a backword search in text file i mean (by backword search) to begin the search from the last for example (i will go to school to learn more....) i want to find the last time the word (to) happened then the next backwordly
1
1742
by: ogo796 | last post by:
Hi everyone i want to search the text file,using pregmatch() but it seems like it dose't serch.maybe my serching pattern is not right.look at the following code. Myfile look like this -------------------------------- file : (49/06/01) simon 13; file : (68/05/01) John 17 ; i want to serch simon 13 on the text file. can anyone help me with pregmatch patterns.
4
8422
by: LizAmmo | last post by:
For a school project i am attempting to create a register and login section of a program. i have managed to create the register by writing user names and passwords to a text document, as we cannot use access databases. however in order to do the actual login i have no idea on how to search the text file for a specific password. All my inputs are in text boxes, and i was wondering if anyone could be any help.
0
1915
by: Kakashi | last post by:
Well I want to search a text file that has some text in it and I want to use a pattern to serach for it. My code does this pretty well for single letters or numbers, but when I try to put in a string "Gus" for instance, it says there are no matches at all even though I know there is at least one Gus in the text file which I attached. At first I thought maybe the break statement is cuaseing the problem, but that is not the case, becuase with out...
0
9901
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11031
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10684
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10371
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9517
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4159
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.