String manipulation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ern

    #1

    String manipulation

    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

    #2
    Re: String manipulation

    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 ?

    Comment

    • Vladimir S. Oka

      #3
      Re: String manipulation

      ern wrote:[color=blue]
      > 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 ?
      >[/color]

      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.

      Comment

      • ern

        #4
        Re: String manipulation


        Vladimir S. Oka wrote:[color=blue]
        >
        >
        > Try using "\n" as the second parameter.[/color]


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

        Comment

        • Vladimir S. Oka

          #5
          Re: String manipulation

          ern wrote:[color=blue]
          > Vladimir S. Oka wrote:[color=green]
          >>
          >> Try using "\n" as the second parameter.[/color]
          >
          >
          > Thanks, that worked... now I just need help with my original problem...
          >[/color]

          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.

          Comment

          • Vladimir S. Oka

            #6
            Re: String manipulation

            ern wrote:[color=blue]
            > Vladimir S. Oka wrote:[color=green]
            >>
            >> Try using "\n" as the second parameter.[/color]
            >
            >
            > Thanks, that worked... now I just need help with my original problem...
            >[/color]
            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.

            Comment

            • Vladimir S. Oka

              #7
              Re: String manipulation

              Vladimir S. Oka wrote:[color=blue]
              > ern wrote:[color=green]
              >> Vladimir S. Oka wrote:[color=darkred]
              >>>
              >>> Try using "\n" as the second parameter.[/color]
              >>
              >>
              >> Thanks, that worked... now I just need help with my original problem...
              >>[/color]
              > 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.[/color]

              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.

              Comment

              • ern

                #8
                Re: String manipulation


                Vladimir S. Oka wrote:[color=blue]
                > ern wrote:[color=green]
                > > Vladimir S. Oka wrote:[color=darkred]
                > >>
                > >> Try using "\n" as the second parameter.[/color]
                > >
                > >
                > > Thanks, that worked... now I just need help with my original problem...
                > >[/color]
                >
                > 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.[/color]

                When did I ask for you to write my code ?

                Comment

                • Vladimir S. Oka

                  #9
                  Re: String manipulation

                  ern wrote:[color=blue]
                  > Vladimir S. Oka wrote:[color=green]
                  >> ern wrote:[color=darkred]
                  >>> Vladimir S. Oka wrote:
                  >>>> Try using "\n" as the second parameter.
                  >>>
                  >>> Thanks, that worked... now I just need help with my original problem...
                  >>>[/color]
                  >> 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.[/color]
                  >
                  > When did I ask for you to write my code ?
                  >[/color]

                  I have apologised/corrected myself elsethread.

                  Cheers

                  Vladimir


                  --
                  My e-mail address is real, and I read it.

                  Comment

                  • ern

                    #10
                    Re: String manipulation


                    Vladimir S. Oka wrote:[color=blue]
                    > ern wrote:[color=green]
                    > > Vladimir S. Oka wrote:[color=darkred]
                    > >> 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.[/color]
                    > >
                    > > When did I ask for you to write my code ?
                    > >[/color]
                    >
                    > I have apologised/corrected myself elsethread.
                    >
                    > Cheers
                    >
                    > Vladimir
                    >
                    >
                    > --
                    > My e-mail address is real, and I read it.[/color]

                    Thanks for the help.

                    Comment

                    Working...