473,543 Members | 1,807 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 63258
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
1519
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
2203
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
4386
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
1546
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...
3
1504
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
2333
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,...
1
1173
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
1729
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...
4
8340
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...
0
1656
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...
0
7594
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. ...
1
7354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7693
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...
0
5888
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...
0
4898
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...
0
3394
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3394
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
643
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...

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.