String manipulation

Discussion in 'C Programming' started by ern, Jan 19, 2006.

  1. ern

    ern Guest

    I have a <char * Buffer> with the following data:

    "
    ; This is a comment

    preprocess:

    play music ; comment

    start:

    ; This is a comment

    turn on
    fast forward

    repeat:
    "
    I want to capture each line one at a time, determine if there is a
    comment (and where it is, because some lines may have a command and a
    comment), then execute the command. I was looking at strtok( , ), but
    I just want to get a line one at a time. Is there some kind of
    "getline( )" function ? Anybody have design tips for this problem ?
    Thanks !
    ern, Jan 19, 2006
    #1
    1. Advertising

  2. ern

    ern Guest

    Now when I try to use strtok( ) to capture a l ine from "Buffer", I get
    an unhandled exception and the program crashes. Here's my code:

    char * lineOfScript;
    lineOfScript = strtok(Buffer,'\n'); // Is the '\n' not a valid
    delimeter ?
    ern, Jan 19, 2006
    #2
    1. Advertising

  3. ern wrote:
    > Now when I try to use strtok( ) to capture a l ine from "Buffer", I get
    > an unhandled exception and the program crashes. Here's my code:
    >
    > char * lineOfScript;
    > lineOfScript = strtok(Buffer,'\n'); // Is the '\n' not a valid
    > delimeter ?
    >


    Please quote posts you reply to -- even your own...

    '\n' is a valid line delimiter, but is not a valid string. strtok()
    expects a pointer to a C string as it's second parameter:

    char *strtok(char *s, const char *delim);

    Your compiler should have whined about converting a char to a char*. Did it?

    Your program most likely crashed since a char constant was converted to
    a pointer to char which most likely resulted in an invalid pointer. Once
    you (or strtok() in your case) try to dereference such a pointer,
    anything can happen.

    Try using "\n" as the second parameter.

    Cheers

    Vladimir



    --
    My e-mail address is real, and I read it.
    Vladimir S. Oka, Jan 19, 2006
    #3
  4. ern

    ern Guest

    Vladimir S. Oka wrote:
    >
    >
    > Try using "\n" as the second parameter.



    Thanks, that worked... now I just need help with my original problem...
    ern, Jan 19, 2006
    #4
  5. ern wrote:
    > Vladimir S. Oka wrote:
    >>
    >> Try using "\n" as the second parameter.

    >
    >
    > Thanks, that worked... now I just need help with my original problem...
    >

    Ah sorry, you originally wanted design tips (not strictly topical, but
    hey...):

    Use fgets() or similar to read in one line at a time.
    Use strtok() to parse the lines you've read.

    Hope this helps.

    Cheers

    Vladimir


    --
    My e-mail address is real, and I read it.
    Vladimir S. Oka, Jan 19, 2006
    #5
  6. ern wrote:
    > Vladimir S. Oka wrote:
    >>
    >> Try using "\n" as the second parameter.

    >
    >
    > Thanks, that worked... now I just need help with my original problem...
    >


    If by that you mean "can you (please) write this code for me", I'm
    afraid I (and probably most people here) am going to disappoint you.
    You're doing fine with strtok() for the moment, it seems. Give it a
    stab, and if it doesn't work as you expect post your try, and people may
    be able to help you fix it.

    Pointer to get you started: look up fgets() function for reading lines
    of text one at a time (do NOT use gets(), it's dangerous -- see some
    other threads here as to why exactly).

    Cheers

    Vladimir


    --
    My e-mail address is real, and I read it.
    Vladimir S. Oka, Jan 19, 2006
    #6
  7. Vladimir S. Oka wrote:
    > ern wrote:
    >> Vladimir S. Oka wrote:
    >>>
    >>> Try using "\n" as the second parameter.

    >>
    >>
    >> Thanks, that worked... now I just need help with my original problem...
    >>

    > Ah sorry, you originally wanted design tips (not strictly topical, but
    > hey...):
    >
    > Use fgets() or similar to read in one line at a time.
    > Use strtok() to parse the lines you've read.


    This happens when one has to jump around to see the original post. I've
    just realised you have a multiline file read into a C string, and you
    want it tokenised. Well, strtok() should do fine for that purpose,
    fgets() won't be useful.

    However, I'd question the design choice of reading the whole multiline
    file into a single string. Reading it line by line in the first place
    might have been better, hence my fgets() musings.

    Cheers

    Vladimir


    --
    My e-mail address is real, and I read it.
    Vladimir S. Oka, Jan 19, 2006
    #7
  8. ern

    ern Guest

    Vladimir S. Oka wrote:
    > ern wrote:
    > > Vladimir S. Oka wrote:
    > >>
    > >> Try using "\n" as the second parameter.

    > >
    > >
    > > Thanks, that worked... now I just need help with my original problem...
    > >

    >
    > If by that you mean "can you (please) write this code for me", I'm
    > afraid I (and probably most people here) am going to disappoint you.


    When did I ask for you to write my code ?
    ern, Jan 20, 2006
    #8
  9. ern wrote:
    > Vladimir S. Oka wrote:
    >> ern wrote:
    >>> Vladimir S. Oka wrote:
    >>>> Try using "\n" as the second parameter.
    >>>
    >>> Thanks, that worked... now I just need help with my original problem...
    >>>

    >> If by that you mean "can you (please) write this code for me", I'm
    >> afraid I (and probably most people here) am going to disappoint you.

    >
    > When did I ask for you to write my code ?
    >


    I have apologised/corrected myself elsethread.

    Cheers

    Vladimir


    --
    My e-mail address is real, and I read it.
    Vladimir S. Oka, Jan 20, 2006
    #9
  10. ern

    ern Guest

    Vladimir S. Oka wrote:
    > ern wrote:
    > > Vladimir S. Oka wrote:
    > >> ern wrote:
    > >>> Vladimir S. Oka wrote:
    > >>>> Try using "\n" as the second parameter.
    > >>>
    > >>> Thanks, that worked... now I just need help with my original problem...
    > >>>
    > >> If by that you mean "can you (please) write this code for me", I'm
    > >> afraid I (and probably most people here) am going to disappoint you.

    > >
    > > When did I ask for you to write my code ?
    > >

    >
    > I have apologised/corrected myself elsethread.
    >
    > Cheers
    >
    > Vladimir
    >
    >
    > --
    > My e-mail address is real, and I read it.


    Thanks for the help.
    ern, Jan 20, 2006
    #10
    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. jared in ecs

    Perl string manipulation

    jared in ecs, Oct 22, 2003, in forum: Perl
    Replies:
    2
    Views:
    799
    Roy Johnson
    Oct 22, 2003
  2. Garfield

    String Array Manipulation Problem

    Garfield, Aug 27, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    2,801
    Michael Lang
    Aug 27, 2003
  3. Aaron

    String Manipulation

    Aaron, Dec 31, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    4,610
    Dave Davidson
    Dec 31, 2003
  4. Aaron

    Advanced String Manipulation (C#)

    Aaron, Jan 2, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    806
  5. Chris

    String manipulation

    Chris, May 24, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    440
    Pete Wright
    May 24, 2004
Loading...

Share This Page