Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > String manipulation

Reply
Thread Tools

String manipulation

 
 
ern
Guest
Posts: n/a
 
      01-19-2006
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 !

 
Reply With Quote
 
 
 
 
ern
Guest
Posts: n/a
 
      01-19-2006
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 ?

 
Reply With Quote
 
 
 
 
Vladimir S. Oka
Guest
Posts: n/a
 
      01-19-2006
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.
 
Reply With Quote
 
ern
Guest
Posts: n/a
 
      01-19-2006

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



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

 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      01-19-2006
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.
 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      01-19-2006
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.
 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      01-19-2006
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.
 
Reply With Quote
 
ern
Guest
Posts: n/a
 
      01-20-2006

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 ?

 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      01-20-2006
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.
 
Reply With Quote
 
ern
Guest
Posts: n/a
 
      01-20-2006

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String manipulation Chris ASP .Net 1 05-24-2004 02:19 PM
Advanced String Manipulation (C#) Aaron ASP .Net 2 01-02-2004 06:50 PM
String Manipulation Aaron ASP .Net 3 12-31-2003 03:39 PM
Perl string manipulation jared in ecs Perl 2 10-22-2003 05:36 PM
String Array Manipulation Problem Garfield ASP .Net 5 08-27-2003 10:07 PM



Advertisments