Search Through String

This is a discussion on Search Through String within the C++ Programming forums, part of the General Programming Boards category; I'm having some trouble with reading in words from a file, and separating them into a multi-dimensional vector. I am ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    19

    Search Through String

    I'm having some trouble with reading in words from a file, and separating them into a multi-dimensional vector.

    I am able to read the words in, but I need to be able to find where a space or carriage return occurs in the .txt file so I can separate each word and store them separately.

    Code:
              ifstream OpenFile("example.txt");
    
    		int pos = 0;
            string str;
           while(!OpenFile.eof())
           {
                    getline(OpenFile, str);
                    cout << str;
    
    				string::size_type pos = str.find_first_of(" ");
    
    			while (pos != string::npos) {
    			cout << "Found space at: " << pos << endl;
    			 pos = str.find_first_of(" .", pos + 1);
    			}
    				
          }
            OpenFile.close();
    That finds the space, but I don't know how to delete it.

    Thanks for your help.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Perhaps you can find something here: http://cppreference.com/cppstring/index.html

    I think erase() might work.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Can you read in with operator>> instead of getline? operator>> automatically stops at and ignores whitespace. Of course, this only works for whitespace, so if you need to handle punctuation in the same way then I guess getline is the way to go.

    You can always create a substring with the current word using substr, and then erase that word and the trailing whitespace before continuing your loop.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    Quote Originally Posted by Daved View Post
    You can always create a substring with the current word using substr, and then erase that word and the trailing whitespace before continuing your loop.
    That's what I was initially trying to do, but I don't know how to use the substr function to do something like that.

    Would I add a for loop inside of the second while loop, and have substr somehow delete the position where the space or carriage return was found?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    I don't think you need another loop. I was thinking of using substr to get the good word. Then you can either use erase or substr again to get the rest of the line without that word.

    You could use find_first_not_of to find the first regular character after the whitespace. That would be the first character of the next good word. Use that as the start of the rest of the line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

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