473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

find word in file

37
Hi i have been trying to solve this question:

Write a C++ program to find a given word in a file. It should display all the line numbers where the word occurs.

i did this code:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. string s1;
  10. ifstream in ("file.txt");
  11. int lines = 0;
  12. cout << "Enter your search string: " <<endl;
  13. cin >> s1;
  14.  
  15. while(!in.eof())
  16. {
  17.    in.seekg(0,ios::beg);
  18.    lines ++;
  19.    if(//String in file)
  20.    {
  21.  
  22.    cout<<"The  string occured at lines: " << lines<<endl;
  23.   }
  24. }
  25.  
  26. in.close();
  27.  
  28. getch();
  29. return 0;
  30. }
i just have problems in interpreting this part to code:
if(//String in file), hope that someone can help me fix it.

thanks

carly
Dec 8 '06 #1
3 7963
this site has info about the ios::beg
http://www.cplusplus.com/doc/tutorial/files.html
it should be able to help you thru the search
I've used those funct.'s before so I can't really help without rewrite the program
but i'm assuming you need it to work with the one you posted right?
Dec 8 '06 #2
it also covers the " in.seekg(0,ios::beg) "
which you'll probably use to read each line and then maybe dump it in to a string (which might be easier to compare) and compare sl ( the user's string) with each character in the file...
like if( sl[x] == (Character from File) )...
well something like that, just make sure you add so error coding tho...
Dec 8 '06 #3
carly
37
this site has info about the ios::beg
http://www.cplusplus.com/doc/tutorial/files.html
it should be able to help you thru the search
I've used those funct.'s before so I can't really help without rewrite the program
but i'm assuming you need it to work with the one you posted right?
ummm... I only needed help in writing this part to code:
Expand|Select|Wrap|Line Numbers
  1. if(//String in file)
, the tutorials you gave me didnt really help me in figuring out how i can translate that sentence to code, but thanks anyway

carly
Dec 8 '06 #4

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

Similar topics

1
by: t0M | last post by:
It's nearly impossible to find anything on this because of the Dictionary class, included within the dotnet framework, that pollutes any search results pertinent to my question. I want to be...
1
by: James Vitale | last post by:
Using vb asp.net 1.1 I'm doing a word automation on a doc file and trying to do a find and replace. My existing code works fine except that it doesn't find and replace in the header. My code...
2
by: Al_P via DotNetMonster.com | last post by:
I have: Win2K Office2000 Working in VB.Net (2003) My .Net project has a reference to Word 9.0 Instantiating things in a simple and straightforward manner: Dim word as Word.Application Dim...
1
by: Jordan | last post by:
Hi. I am autoamtiacally creating a MS Word file through code. Most of my code works, but when I try to do a find and replace, I get a read/write memrory access error. Not sure what is going on. Any...
2
by: Perl Beginner | last post by:
Hi, Is there a way to find a line in a text file that starts with a forward slash? This is how i find a line in text file that starts with a word and then print that line to another text file: ...
18
by: Neehar | last post by:
Hello For one of the interviews I took recently, I was given an offline programming quiz. In 30 minutes I had to write code in C++ to counts the number of times each unique word appears in a...
3
by: Soulless | last post by:
Hi, I have created a batch file in C# and want to find and place it in the path of an installed application. Now, this might be done on an XP, 2000, etc system. The application is installed...
1
by: amiparna | last post by:
There is two frame in my form.In d first frame there is a text box,and i give some word in the text box.I want to search the word from a file from the location(http://45.12.09.31...........) and if...
1
by: xetulul | last post by:
my problem is that im unable to match a user input word to a word in a string. the strings are in a file and then placed in nodes. i have to go through each token in each node to find the 'word'....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.