Some help with find and get

This is a discussion on Some help with find and get within the C++ Programming forums, part of the General Programming Boards category; Hi all. I'm curious if there's a way to use the find function to find a certain character, then the ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    27

    Some help with find and get

    Hi all. I'm curious if there's a way to use the find function to find a certain character, then the get function to get that complete string? For example, if I were to find the "@" in a file and then extract the surrounding string with the get command? How do I go about doing something like this? Thanks!

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Sure, first use:

    find()

    then use

    substr()
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    No.

    But you can always write some code to do that. You could use getline() to read the whole file into a string variable. Then you could use find() to get the index position of "@". Then you can test the characters before that index value until you find a space and record that index value. Then you can test each character after that index value until you find a space, comma, colon, period, etc. and record that index position. Finally, you can use substr() to get the substring between those two index values.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    Ah I see, you mean get the word that contains the @. Yes, that's more complicated. You'd have to do as 7stud mentioned.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    27
    OK, so in order for me to get the info I want I would need to:
    1) use getline() to put my file into string
    2) use find() to get the position of "@"
    3) run some kind of test to pick up the characters before and after the @ to store the index points between white characters
    4) use substr to store the data between the index values in 3

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    run some kind of test to pick up the characters before and after the @
    It's called a while loop. Think: --

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I would just read the file in one word at a time and if the word read in has the '@', then you're done. You don't need to store the rest of the file if all you want is the word with the '@'. It gets more complicated depending on your definition of word. My simple solution assumes a whitespace delimited word that can be read in with operator>>.

Popular pages Recent additions subscribe to a feed

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