473,659 Members | 2,836 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Search a particular string in a file

2 New Member
I need to write a program to search a particular string in a specific file.e.g if the file is abc.dat which contains names of students in a class, I need to search for a particular name say John in that file.

One way to do that might be using the grep functionality but that will restrict the program to linux/unix only

Another way is to use the library function read() to read the entire contents into a buffer of size equal to the size of the file and then search for that particular strring. But I don't want to do that because if the file is too big unneccessary lot of memory space is required for the buffer to be allocated

So what i am looking here is any guidance which will help me to write the above program in an efficient way.
Apr 4 '07 #1
3 3507
faceboy
4 New Member
Run This code and maybe it will help u

code removed per FAQ
Apr 4 '07 #2
Coldfire
289 Contributor
I need to write a program to search a particular string in a specific file.e.g if the file is abc.dat which contains names of students in a class, I need to search for a particular name say John in that file.

One way to do that might be using the grep functionality but that will restrict the program to linux/unix only

Another way is to use the library function read() to read the entire contents into a buffer of size equal to the size of the file and then search for that particular strring. But I don't want to do that because if the file is too big unneccessary lot of memory space is required for the buffer to be allocated

So what i am looking here is any guidance which will help me to write the above program in an efficient way.
out of the plenty of options for file I/O in C++..........u can use this


read word by word

Expand|Select|Wrap|Line Numbers
  1. string sText;
  2. ifstream in("abc.dat");
  3.  
  4. while( in >> sText ) //will read space-delimited one word on each iteration
  5. {
  6.     cout << "Read a word " << sText ;
  7.     // Here you can do your requied string comparison
  8.     //(sText==Name) - Bingo!
  9. }
  10.  
The above code will work like :
abc.dat contains e.g.
John is studying in class 10

on ist while iteration:
sText=John
on ist while iteration:
sText=is
on ist while iteration:
sText=studying
on ist while iteration:
sText=in
on ist while iteration:
sText=class
on ist while iteration:
sText=10

another way is to read file using getline func()
Expand|Select|Wrap|Line Numbers
  1.  
  2. char chLine[50];//50 is line length
  3. ifstream in("abc.dat");
  4.  
  5. while( in.getline(chLine,50) ) 
  6. {
  7.     cout << "Read line from file: " << chLine;
  8.     // after reading one line at a time u can do whatever u want from that line
  9. }
  10.  

......

Good Day
Coldfire
_______________ ______
GetOneByOne CS eBooks
Apr 4 '07 #3
Cplusplusbeginner
2 New Member
out of the plenty of options for file I/O in C++..........u can use this


read word by word

Expand|Select|Wrap|Line Numbers
  1. string sText;
  2. ifstream in("abc.dat");
  3.  
  4. while( in >> sText ) //will read space-delimited one word on each iteration
  5. {
  6.     cout << "Read a word " << sText ;
  7.     // Here you can do your requied string comparison
  8.     //(sText==Name) - Bingo!
  9. }
  10.  
The above code will work like :
abc.dat contains e.g.
John is studying in class 10

on ist while iteration:
sText=John
on ist while iteration:
sText=is
on ist while iteration:
sText=studying
on ist while iteration:
sText=in
on ist while iteration:
sText=class
on ist while iteration:
sText=10

another way is to read file using getline func()
Expand|Select|Wrap|Line Numbers
  1.  
  2. char chLine[50];//50 is line length
  3. ifstream in("abc.dat");
  4.  
  5. while( in.getline(chLine,50) ) 
  6. {
  7.     cout << "Read line from file: " << chLine;
  8.     // after reading one line at a time u can do whatever u want from that line
  9. }
  10.  

......

Good Day
Coldfire
_______________ ______
GetOneByOne CS eBooks

Thanks! Coldfire it really helped .Thanks! once more
Apr 5 '07 #4

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

Similar topics

3
2505
by: Carol | last post by:
What's the best way to do a site search of my site? I am using asp and access heavily on a regular ISP google? Or write a bunch of queries? what else is there?
60
49061
by: Julie | last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB) for a given string. The files are unindexed and unsorted, and for the purposes of my immediate requirements, can't be indexed/sorted. I don't want to load the entire file into physical memory, memory-mapped files are ok (and preferred). Speed/performance is a requirement -- the target is to locate the string in 10 seconds or less for a 100 MB file. The...
32
14837
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if ((someString.IndexOf("something1",0) >= 0) || ((someString.IndexOf("something2",0) >= 0) ||
3
9548
by: Chung Leong | last post by:
Here's the rest of the tutorial I started earlier: Aside from text within a document, Indexing Service let you search on meta information stored in the files. For example, MusicArtist and MusicAlbum let you find MP3 and other music files based on the singer and album name; DocAuthor let you find Office documents created by a certain user; DocAppName let you find files of a particular program, and so on. Indexing Service uses plug-ins...
1
3638
by: vinothg | last post by:
Hi , I have a binary file which contains 30,000 strings of 20 bytes each.I need to search for a string in the file to see whether the particular string exists. The sample code which i wrote to write a single string to binary file is : #include <iostream> #include <fstream> int main () {
10
2239
by: B. Williams | last post by:
I have an assignment that requires me to write a program that uses a class, a constructor, a switch, and store the records in a text file. The second requirement is to create a function called updatePower which will search through the file looking at the names and if a name is a match, will allow you to replace the integer stored for power. I have written the program and completed the first task, but I need some assistance on the secind....
4
2506
by: ravindarjobs | last post by:
hi...... i am using ms access 2003,vb6 i have a form. in that i have 2 buttons 1. start search 2 stop search when i click the "start search" button the fucntion SearchSystem() is called, it will search for a particular file in the computer(searches entire drives).
2
2657
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. creating a method to search a tree to find the position of node and to return its pointer value basically i need to read in a text file... shown below H H,E,L E,B,F B,A,C A,null,null
0
10752
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
8427
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
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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
8746
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
8627
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
7356
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...
1
6179
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2750
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.