strtok challenge

Discussion in 'C Programming' started by Sheldon, Nov 6, 2007.

  1. Sheldon

    Sheldon Guest

    Hi Everyone,

    I have a file with a set of strings per line and each line ends with a
    '\n' and each string separated by a whitespace.
    After reading each line and using strtok() to split the string into 3
    strings, the last string is saved with the '\n' character still
    attached. I would like to know if strtok can be used to fix this
    problem or do I have to use strlen and and if statement to solve this?

    Any help is appreciated!

    /S
     
    Sheldon, Nov 6, 2007
    #1
    1. Advertising

  2. Sheldon

    John Gordon Guest

    In <> Sheldon <> writes:

    > I have a file with a set of strings per line and each line ends with a
    > '\n' and each string separated by a whitespace.
    > After reading each line and using strtok() to split the string into 3
    > strings, the last string is saved with the '\n' character still
    > attached. I would like to know if strtok can be used to fix this
    > problem or do I have to use strlen and and if statement to solve this?


    You could include '\n' in strtok's separator string and call strtok an
    additional time.

    Or you could remove the '\n' when you first read the line from your input,
    before you even call strtok.

    Generally, any program that deals with line-oriented input needs to decide
    if it cares about the newline character at the end of every line. If not,
    it's generally best to strip it out immediately after reading the line,
    before the line is processed.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Nov 6, 2007
    #2
    1. Advertising

  3. Sheldon

    santosh Guest

    On Tuesday 06 Nov 2007 11:16 pm Sheldon <> wrote in
    article <>:

    > Hi Everyone,
    >
    > I have a file with a set of strings per line and each line ends with a
    > '\n' and each string separated by a whitespace.


    A string in C is defined as being terminated by a null character.
    Sequences of characters delimited by whitespace do not meet C's
    definition of a string.

    > After reading each line and using strtok() to split the string into 3
    > strings, the last string is saved with the '\n' character still
    > attached. I would like to know if strtok can be used to fix this
    > problem or do I have to use strlen and and if statement to solve this?


    Yes, strtok() can be sufficient for this. Just supply the newline
    character in the delimiter string supplied to strtok().
     
    santosh, Nov 6, 2007
    #3
  4. Sheldon

    Eric Sosman Guest

    John Gordon wrote On 11/06/07 13:00,:
    > In <> Sheldon <> writes:
    >
    >
    >>I have a file with a set of strings per line and each line ends with a
    >>'\n' and each string separated by a whitespace.
    >>After reading each line and using strtok() to split the string into 3
    >>strings, the last string is saved with the '\n' character still
    >>attached. I would like to know if strtok can be used to fix this
    >>problem or do I have to use strlen and and if statement to solve this?

    >
    >
    > You could include '\n' in strtok's separator string


    Yes.

    > and call strtok an
    > additional time.


    No need.

    --
     
    Eric Sosman, Nov 6, 2007
    #4
  5. Sheldon

    jason Guest

    On Tue, 06 Nov 2007 17:46:53 +0000, Sheldon wrote:

    > Hi Everyone,
    >
    > I have a file with a set of strings per line and each line ends with a
    > '\n' and each string separated by a whitespace. After reading each line
    > and using strtok() to split the string into 3 strings, the last string
    > is saved with the '\n' character still attached. I would like to know if
    > strtok can be used to fix this problem or do I have to use strlen and
    > and if statement to solve this?
    >
    > Any help is appreciated!
    >
    > /S


    Hello /S use \n in your delimiter array.

    Jas.
     
    jason, Nov 6, 2007
    #5
  6. Sheldon

    Sheldon Guest

    On 6 Nov, 19:00, John Gordon <> wrote:
    > In <> Sheldon <> writes:
    >
    > > I have a file with a set of strings per line and each line ends with a
    > > '\n' and each string separated by a whitespace.
    > > After reading each line and using strtok() to split the string into 3
    > > strings, the last string is saved with the '\n' character still
    > > attached. I would like to know if strtok can be used to fix this
    > > problem or do I have to use strlen and and if statement to solve this?

    >
    > You could include '\n' in strtok's separator string and call strtok an
    > additional time.
    >
    > Or you could remove the '\n' when you first read the line from your input,
    > before you even call strtok.
    >
    > Generally, any program that deals with line-oriented input needs to decide
    > if it cares about the newline character at the end of every line. If not,
    > it's generally best to strip it out immediately after reading the line,
    > before the line is processed.
    >
    > --
    > John Gordon A is for Amy, who fell down the stairs
    > B is for Basil, assaulted by bears
    > -- Edward Gorey, "The Gashlycrumb Tinies"


    thanks!
    It works with strtok(line, " \n");
    /S
     
    Sheldon, Nov 6, 2007
    #6
  7. Sheldon

    John Gordon Guest

    In <1194372436.531526@news1nwk> Eric Sosman <> writes:

    > > and call strtok an additional time.


    > No need.


    Quite right. Not sure why I thought that was needed.

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Nov 6, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Adam Balgach
    Replies:
    2
    Views:
    567
    news-east
    Nov 28, 2004
  2. Alex Vinokur

    strtok() and std::string

    Alex Vinokur, Apr 14, 2005, in forum: C++
    Replies:
    6
    Views:
    4,927
    Pete Becker
    Apr 14, 2005
  3. strtok problem

    , Aug 28, 2003, in forum: C Programming
    Replies:
    4
    Views:
    509
  4. Robert

    strtok trouble

    Robert, Sep 5, 2003, in forum: C Programming
    Replies:
    17
    Views:
    1,228
    Jalapeno
    Sep 6, 2003
  5. Fatih Gey

    segfault on strtok

    Fatih Gey, Oct 23, 2003, in forum: C Programming
    Replies:
    40
    Views:
    1,449
    nobody
    Nov 1, 2003
Loading...

Share This Page