Solved

searching for strings

Posted on 1998-07-13
20
206 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
Comment Utility
Use strcspn, wcscspn, _mbscspn.

0
 
LVL 8

Expert Comment

by:trestan
Comment Utility
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
Comment Utility
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
 

Author Comment

by:tetsuo
Comment Utility
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
Comment Utility
For a 420 bytes file, my method is no problem. They are ANSI routines.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
If you like some sample code, pls tell me your email address. Mine can be found in my homepage(click on my name).
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

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

0
 
LVL 22

Expert Comment

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

Expert Comment

by:laeuchli
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now