473,394 Members | 1,671 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,394 software developers and data experts.

Search a particular string in a file

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 3499
Run This code and maybe it will help u

code removed per FAQ
Apr 4 '07 #2
Coldfire
289 100+
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
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
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
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...
32
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...
3
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...
1
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...
10
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...
4
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,...
2
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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,...
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
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...
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...

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.