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

Home Posts Topics Members FAQ

String search

1 New Member
C++ string search
--------------------------------------------------------------------------------

Hello, please can any one help me? I am given an assigment in C++ to read a text file and search for certain words.If those stated words are found, then I should print the enire them including the entire line. Below is an example of such a text file


Example text file:

Possible failures:
-> Member=1, FSce=1, MP=13.16%, [M]atching (5/5)=100%, [P]rediction (5/38)=13.16%,
input SA0 at port S0 of
instance=ip_ins t.pip_kau_i.ins t_pip_kau_dp.in st_ka sumi.dp.keysche duler.i_17
(num=82723)
x=3134600 y=2549120 x=3136840 y=2553040 orientation=FS,
module=mx21i1x1 v net=ip_inst.pip _kau_i.s_cikey_ reg[16] (num=58177),
layer=M2 (x1=3137540 y1=2551360 x2=3137540 y2=2552200),
layer=M2 (x1=3140340 y1=2552200 x2=3140340 y2=2554440),
layer=M2 (x1=3140340 y1=2554440 x2=3140340 y2=2559200),
layer=M3 (x1=3136140 y1=2559200 x2=3140340 y2=2559200),
layer=M3 (x1=3137540 y1=2552200 x2=3140340 y2=2552200),
layer=M3 (x1=3140340 y1=2559200 x2=3145100 y2=2559200)
ToggleLowPat=<n one> ToggleHighPat=7 0 (FailureSet=171 745);


::::::::::::::: ::::::::::::::: ::::::::::::::: ::::: ::::::::::::::: ::::::::::::::: ::::::::::::::: :::::
Possible failures:
-> Member=1, FSce=2, MP=4.76%, [M]atching (5/5)=100%, [P]rediction (5/105)=4.76%,
output SA0 at port Q of instance=ip_ins t.pip_kau_i.ins t_reg.ucc_key0_ reg_r eg_16
(num=96536)
x=3130120 y=2556960 x=3136280 y=2560880 orientation=FS,
module=df7sqx1v net=ip_inst.pip _kau_i.s_cikey_ reg[16] (num=58177),
layer=M2 (x1=3137540 y1=2551360 x2=3137540 y2=2552200),
layer=M2 (x1=3140340 y1=2552200 x2=3140340 y2=2554440),
layer=M2 (x1=3140340 y1=2554440 x2=3140340 y2=2559200),
layer=M3 (x1=3136140 y1=2559200 x2=3140340 y2=2559200),
layer=M3 (x1=3137540 y1=2552200 x2=3140340 y2=2552200),
layer=M3 (x1=3140340 y1=2559200 x2=3145100 y2=2559200)
ToggleLowPat=2 ToggleHighPat=7 0,
Internal Fault at instance=ip_ins t.pip_kau_i.ins t_reg.ucc_key0_ reg_r eg_16,
module=df7sqx1v
x=3130120 y=2556960 x=3136280 y=2560880 orientation=FS (FailureSet=250 955);


::::::::::::::: ::::::::::::::: ::::::::::::::: ::::: ::::::::::::::: ::::::::::::::: ::::::::::::::: :::::


So my question is how can I read each paragraph separately for example, in paragraph 1, I want to search for "Member=1, FSce=1" and later "instance=" . For each of these words found, I want to print the entire line where each lies.
Below is the C++ code I started but get stock midway.I can only search for "Member=1, FSce=1" but the other.
I'm interestted on how to read and stop within paragraphs or read until a specified delimiter is met, and do the search.

My code
Expand|Select|Wrap|Line Numbers
  1. #include <algorithm>
  2. #include <sstream>
  3. #include <fstream>
  4. #include <string>
  5. #include <cstring>
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. int linecount;
  10. int happencount;
  11.  
  12.  
  13. unsigned int findtheword (string token, string line, int drill)
  14. {
  15.    unsigned int gristle = line.find(token, drill);
  16.    if (gristle == string::npos)
  17.    {
  18.       return gristle;
  19.    }
  20.    happencount++;
  21.    findtheword (token, line, (gristle + 1));
  22.    return gristle;
  23. }
  24.  
  25.  
  26. int main(int argc, char *argv[])
  27.  
  28. // argc tells us how many arguments were passed in th array
  29. // argv is effectively an array of pointers to the arguments
  30. // Each argument is passed as a seperate character array,
  31. //so argv[1] points to the first argument text,ie the word to look for
  32. //argv[2] points to the second argument text,ie the file to be open, etc.
  33. //(argv[0] gives you the name of the executable file for the program itself)
  34.  
  35. {
  36.    argv[1] = "Member=1, FSce=1";
  37.  
  38.    argv[2] = "listings/NB0264.00_NB0264-22B6_X2_Y48_dp_bf_X3_plat_max_20070110081127_dp_bf _faloc.lis";
  39.  
  40.    // argv[3] = "instance=";
  41.    if (argc!= 3)
  42.    {
  43.       cout << "Proper usage: findWord <word> <file>\n";
  44.       cout << "where\n";
  45.       cout << " <word> is a sequence of non-whitespace characters\n";
  46.       cout << " <file> is the file in which to search for the word\n";
  47.       cout << "example: findWord the test.txt\n";
  48.       return 0;
  49.    }
  50.  
  51.    ifstream myFile(argv[2]);
  52.    if (myFile)
  53.    { // if the file was successfully opened
  54.       cout << "Searching for 'argv[1]' in file 'argv[2]'\n";
  55.       cout << endl;
  56.  
  57.       string line;
  58.       while (getline(myFile, line))
  59.       {
  60.          linecount ++;
  61.          unsigned int grip = findtheword(argv[1],line,0);
  62.          if (grip != string::npos)
  63.          {
  64.             cout << "argv[1] Found on line"<< ' ' << linecount <<endl;
  65.             cout << line <<endl;
  66.             cout << setfill(';');
  67.          }
  68.      }
  69.  
  70.       cout << "The # of occurrences of ' ";
  71.       cout << argv[1];
  72.       cout << "' = ";
  73.       cout << happencount;
  74.       cout << endl << endl;
  75.  
  76.       cout << "Now searching for 'argv[3]' up to the specified delimiter:\n";
  77.       cout << endl;
  78.  
  79.       while (argv[2] && (getline (myFile, line, ';'))){}
  80.  
  81.       myFile.close(); // close the file now that we're done with it
  82.    }
  83.    else
  84.    {
  85.       cout << "File '";
  86.       cout << argv[2];
  87.       cout << "' could not be opened\n";
  88.    }
  89.    return 0;
  90. }
Jul 17 '07 #1
1 3267
sicarie
4,677 Recognized Expert Moderator Specialist
Looks like you are already using getline() to pull the lines in, have you looked into strtok() or any of the other string.h functions to manipulate what is returned by getline()?
Jul 17 '07 #2

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

Similar topics

10
39347
by: Anand Pillai | last post by:
To search a word in a group of words, say a paragraph or a web page, would a string search or a regexp search be faster? The string search would of course be, if str.find(substr) != -1: domything() And the regexp search assuming no case restriction would be,
4
2671
by: Ken Fine | last post by:
I'm looking to find or create an ASP script that will take a string, examine it for a search term, and if it finds the search term in the string, return the highlighted search term along with the words that surround it. In other words, I want the search term highlighted and shown in an excerpt of the context in which it appears. Any suggestions or pointers? This behavior is most often seen as part of a search engine. In my case, I want...
4
13638
by: Jason Gleason | last post by:
What's the most efficient way to get the number of occurences of a certain string in another string..for instance i'm using the following code right now... private int CharacterCounter(String text,String Character) { int count = 0;
60
49066
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...
15
6021
by: Chuck Bowling | last post by:
I'm having problems doing an efficient keyword search on a text file likely to be smaller than 100k. I have a keyword list of about 200 strings and I need to search the file and mark all of the words in the list. I tried doing a Regex search but it was way too slow. Now I'm trying to do a straight text search on a string array but I can't get it to work. Below is what I tried and I'm not sure what I'm doing wrong. Anybody have a suggestion?...
4
6494
by: Nikos | last post by:
Hi... I would like to search for a hex string (for example: "E903") inside a binary file... Although I open the file correctly, how do I search hex values? Thanks in advance! Nikos
32
14839
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
12986
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast object of type 'System.Byte' to type 'System.String'. here is the code that I used to create a table and then add columns to it later, later I populate the rows in the table.
21
3386
by: gary | last post by:
How would one make the ECMA-262 String.replace method work with a string literal? For example, if my string was "HELLO" how would I make it work in this instance. Please note my square brackets are not regular expression syntax. Thanks,
14
2966
by: S | last post by:
Any idea on how I would be able to do a search within C# that does ranges or words For example I want to search for Chicken in the string string s1 = "This is Great Chicken";
0
8483
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
8402
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,...
1
8605
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8676
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
7445
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
6237
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...
0
5703
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.