search text from file

This is a discussion on search text from file within the C++ Programming forums, part of the General Programming Boards category; Hello, Is there a way to search a vector and return all occurrences of a string. I have read a ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45

    search text from file

    Hello,
    Is there a way to search a vector and return all occurrences of a string. I have read a text file into a vector and want to search throught the vector. For example I am searching the vector for all occurrences with the word ssh. It may be ssh, or it may be ssh me@192.168.0.1. I am not sure if this requires a regexp or some other step to return all occurrences of ssh.
    Thanks for your time,
    Brad

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    So is this a vector of char, containing the entire file as one big array?
    Or a vector of strings, each containing a single line from the file?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    the vector is a vector of strings. I used this to populate it:
    Code:
    while (getline(infile, line))
      {
        file.push_back (line);
      }
    Thanks for the response.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could probably just use
    string::find()


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    I tried using find but it only returns ssh. So if there were 10 occurences of of the word "ssh", they are not all just ssh. Some are ssh me@192.168.0.1. I am trying to return all strings that contain ssh, not just the ones that just have ssh. I thought maybe use an iterator and increment each pass, but it still will only look for ssh, no other variant of it.
    Thanks for the reply.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, you are after finding <nothing|separator>ssh<nothing|separator>, rather than "any string that contains ssh.

    Then you would need some more code. Some [but certainly not ALL possible] alternative solutions:
    1. some sort of regexp parser that you can feed with what is acceptable separators.
    2. write some code that loops through the string (using an iterator, if you wish), and when you find the right separator(s), check if the following string matches the searched for string and that the next thing is a valid separator.
    3. Use the location recieved from find to do a secondary check of the characters before/after the found string, to see if they are acceptable separators or not.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    So, you are after finding <nothing|separator>ssh<nothing|separator>, rather than "any string that contains ssh
    I guess I should have worded it better. The string will always start with ssh, whether it is just ssh or ssh me@192.168.0.1. So in crude regexp format:
    Code:
    ^ssh*
    thanks again

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Am I missing something? Wouldn't
    Code:
    if (1 == str.find("ssh", 1))
    {
        cout << "Found it" << endl;
    }
    work?

  9. #9
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    This is what I have so far.
    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    
    
    using namespace std;
    
    int main(int argc, char** argv)
    {
    	vector<string> phrase;
    	ifstream iFile(".bash_history");
    	string temp;
        if (! iFile)
        {
            cout << "Error opening input file" << endl;
            return -1;
        }
        while (getline(iFile,temp)) {
        	phrase.push_back(temp);
        }
            
        for (int i = 0; i < phrase.size(); i++) {
        	if (1 == phrase.at(i).find("ssh", 1)){
        		cout << "Found it: " << phrase.at(i) << endl;
        		
            }
           
        }
        
    	return 0;
    }

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so you want to check if your find is the first thing in the string?

    Then check if the location returned by find is zero (that is, the first position in the string). If not, then find obviously located the string ssh elsewhere (or not at all, but for the purposes here, we don't care which it is, so treat both as the same).

    It gets a bit more tricky if the string could have spaces or other separators at the beginning - if that's the case, perhaps preprocessing the string and removing those would help a bit.

    Edit: I didn't notice the other posts as I started writing a reply, got sidetracked and only got back a minute ago. I think the rags_to_riches solution is correct, except you should use 0 as result and start position.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Hmmm. Where 0 was search all from the doc I read, I figured string must be using 1 for start position, but apparently not. This seems to find all string starting with ssh:
    Code:
    if (0 == str.find("ssh"))
    {
        cout << "Found it" << endl;
    }

  12. #12
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    I got it to work doing this, not sure if this is the best way or not?
    Code:
    for (int i = 0; i < phrase.size(); i++) {
        	found = phrase.at(i).find("ssh",0);
            if (found != string::npos) {
            	results.assign(phrase.at(i), found, phrase.at(i).size());
        		cout << results << endl;
        	}
            
        }
    This prints out al the lines containing ssh in the first of the string. Is this way ok? Or would you suggest a better way? Also do you know why c++ does not have a regexp lib?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Search text in file
    By MarlonDean in forum C++ Programming
    Replies: 10
    Last Post: 05-16-2008, 04:51 AM
  3. search for text string in a file
    By basenews in forum C++ Programming
    Replies: 2
    Last Post: 05-03-2007, 06:15 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21