Thread: getline to divide

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    18

    getline to divide

    how would i?
    I have array of string which have the following:
    one meaning of one
    two meaning of two
    three meaning of three

    For example, "one" is a word and "meaning of one" is a meaning of one. How would I divide each line into two pieces so I can have word and meaning separately?

    I would like to use getline but don't know how to use it.
    I want to use a first space to separate those into word and meaning.
    Please let me know. thanks

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Use strchr to find the first space in a string.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    There are a number of ways to read up to the first space and then do something else. The easiest of which would be:
    Code:
    if ( fgets ( a, sizeof a, stdin ) != NULL ) {
      if ( sscanf ( a, "%s %[^\n]", b, c ) == 2 )
        printf ( "%s\n%s\n%s\n", a, b, c );
      else
        /* Report error and handle */
    }
    else
      /* Report error and handle */
    But a safer way would be to modify the input so that there is a delimiting character not in the real data which separates the strings that you want, like so:

    one : meaning of one

    You can then use the : as the separator. That way you don't have to worry about the spacing of the input, such as if someone forgets to put a space between "one" and "meaning":

    one-meaning of one

    This would result in "one-meaning" being the word entry and "of one" being the definition. Not quite what you want.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    18
    thank you, guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 11:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 02:59 PM