Solved

searching for strings

Posted on 1998-07-13
20
212 Views
Last Modified: 2010-04-01
i need to search a text file to determine whether it contains a specific string of text.  can anyone tell me how this can be accomplished?
0
Comment
Question by:tetsuo
  • 7
  • 7
  • 5
  • +1
20 Comments
 
LVL 8

Expert Comment

by:trestan
ID: 1167659
Use strcspn, wcscspn, _mbscspn.

0
 
LVL 8

Expert Comment

by:trestan
ID: 1167660
You can open the file to be searched. Input the contents in a string and use the functions to search for the specific substring. Finally, close the file.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1167661
That works well as long as the file to be searched is not so large that if can't reasonably fit in a string.  That can easily be a problem in a 16 bit program.  In a 32 bit program it is not quite as big a problem.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tetsuo
ID: 1167662
i'm not sure.  i'm working with windows 3.1 because my application is supposed to work with all windows platforms.  the text file is an ini file probably about 420 bytes in length.  any other alternatives?
0
 
LVL 8

Expert Comment

by:trestan
ID: 1167663
For a 420 bytes file, my method is no problem. They are ANSI routines.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1167664
That was definitly the easiest solution.  But if your program is a a 16 bit program AND the file will be long (more than 1K or so) then that would not be a good approach.

The other approach is much more work, you allocate a buffer (say about 1K)  and read part of the file into the buffer and then search for the string in the buffer, if you find it--great.  If not, you read the next section of the file into the buffer and search that and keep going until you reach the end of the file.  This allows you to handle a large file in sections so that it doesn't take up too much memory.

Now for the bad news.  I lied.  There is one detail I left out.  What happens when the string to be found is positioned so that it starts at the end of one buffer and finishes in the end of the second buffer?  


0
 
LVL 22

Expert Comment

by:nietod
ID: 1167665
There are some very efficient ways to handle that, but they are a pain to program.  I recomend you "cheat".  If the search string is X characters long and is not found in the current buffer, then copy the last X-1 characters to the start of the buffer.  Then read in another section of the file  into the buffer starting after the X-1 characters left over from before.  Then search the whole buffer.  This is less efficient.  But is much faster.

Let me know if you have questions.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1167666
Opps!  I missed the 420.  trestan's solution is definitly the way to go with a short file.  However, the OS provides API functions for dealing with .ini files.  (Like GetProfileString())  Is there a reason you aren't using them.
0
 

Author Comment

by:tetsuo
ID: 1167667
well, the reason i haven't used them is because i was never aware of them.  i am unfamiliar with windows specific c++ (i learned on a friggin mac). i'm a student on a first year co op job and i am kinda learning this stuff as i go. perhaps i should have made it clear in the beginning that i have no skills.  so please talk to me as you would an uneducated five year old, assuming i don't know the correct syntax of anything. sorry.
0
 
LVL 8

Expert Comment

by:trestan
ID: 1167668
If you like some sample code, pls tell me your email address. Mine can be found in my homepage(click on my name).
0
 

Author Comment

by:tetsuo
ID: 1167669
thank you that would be very helpful!  my email is st96c9th@post.drexel.edu

0
 
LVL 22

Expert Comment

by:nietod
ID: 1167670
Also, If you have the windows SDK, then look in the docs for GetProfileString().  
0
 
LVL 5

Expert Comment

by:laeuchli
ID: 1167671
try this:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

bool found(string keyword) {
   // get a filename
   cout << "Enter filename: " << flush;
   string filename;
   cin >> filename;

   // define the infile stream
   ifstream fin( filename.c_str() );

   string temp;
   
   while (fin >> temp) {
      // if found, return true, ending the function immediately
      if (temp == keyword)
         return true;
   }
0
 
LVL 8

Expert Comment

by:trestan
ID: 1167672
There are some problem in the proposed code.
The first is what you will do if the file does not exist.
The second is what if the string to be find is consist of spaces.
The third is that namespace is new feature in C++, old C++ compiler may not surport it.

0
 
LVL 22

Expert Comment

by:nietod
ID: 1167673
Most of those problems are trivial adjustments to the propsed code.  However, the "consists of spaces" exposes the real weakness in the code.  There is no way around that problem with that approach and to make matters worse the problem will occur if the search string even contains spaces  (as is very likely when dealing with .ini files.)  

Trestan's approach is, in general, very innefficient, but it almost certainly the best approach for this case.  It produces short, simple code that will do the job well.
0
 
LVL 8

Expert Comment

by:trestan
ID: 1167674
There is way to deal with the spaces. Although it may be a little bit more complex, it can deal with more conditions (cases). However, things will be much easier if MFC can be used.
0
 

Author Comment

by:tetsuo
ID: 1167675
ok the ini file is no sweat.  trestan's approach combined with GetPrivateProfileString() worked perfectly (thank you).  now what if i'm dealing with batch files?  i need to search for a path statement in autoexec.bat
would trestan's method suffice here?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1167676
Yes, assuming the autoexec.bat file is not very long.  I would not use Trestan's methd of a file longer than about 1 K (in a 16 bit program)  But most autoexecs are a lot smaller than 1 K.
0
 
LVL 8

Accepted Solution

by:
trestan earned 50 total points
ID: 1167677
I think the discussion is near to the end. I would like to propose the following information which uses a most compicated method.
http://www.codeguru.com/string/reg_ex.shtml
0
 

Author Comment

by:tetsuo
ID: 1167678
regular expression searching in c++! that's awesome.  thank you trestan for your time. and thank you nietod.  you have both been very patient and insiteful (insightful?)and a huge help to me.   you guys rule
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question