--------------------------------------------------------------------------------
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
- #include <algorithm>
- #include <sstream>
- #include <fstream>
- #include <string>
- #include <cstring>
- #include <iostream>
- using namespace std;
- int linecount;
- int happencount;
- unsigned int findtheword (string token, string line, int drill)
- {
- unsigned int gristle = line.find(token, drill);
- if (gristle == string::npos)
- {
- return gristle;
- }
- happencount++;
- findtheword (token, line, (gristle + 1));
- return gristle;
- }
- int main(int argc, char *argv[])
- // argc tells us how many arguments were passed in th array
- // argv is effectively an array of pointers to the arguments
- // Each argument is passed as a seperate character array,
- //so argv[1] points to the first argument text,ie the word to look for
- //argv[2] points to the second argument text,ie the file to be open, etc.
- //(argv[0] gives you the name of the executable file for the program itself)
- {
- argv[1] = "Member=1, FSce=1";
- argv[2] = "listings/NB0264.00_NB0264-22B6_X2_Y48_dp_bf_X3_plat_max_20070110081127_dp_bf _faloc.lis";
- // argv[3] = "instance=";
- if (argc!= 3)
- {
- cout << "Proper usage: findWord <word> <file>\n";
- cout << "where\n";
- cout << " <word> is a sequence of non-whitespace characters\n";
- cout << " <file> is the file in which to search for the word\n";
- cout << "example: findWord the test.txt\n";
- return 0;
- }
- ifstream myFile(argv[2]);
- if (myFile)
- { // if the file was successfully opened
- cout << "Searching for 'argv[1]' in file 'argv[2]'\n";
- cout << endl;
- string line;
- while (getline(myFile, line))
- {
- linecount ++;
- unsigned int grip = findtheword(argv[1],line,0);
- if (grip != string::npos)
- {
- cout << "argv[1] Found on line"<< ' ' << linecount <<endl;
- cout << line <<endl;
- cout << setfill(';');
- }
- }
- cout << "The # of occurrences of ' ";
- cout << argv[1];
- cout << "' = ";
- cout << happencount;
- cout << endl << endl;
- cout << "Now searching for 'argv[3]' up to the specified delimiter:\n";
- cout << endl;
- while (argv[2] && (getline (myFile, line, ';'))){}
- myFile.close(); // close the file now that we're done with it
- }
- else
- {
- cout << "File '";
- cout << argv[2];
- cout << "' could not be opened\n";
- }
- return 0;
- }