String manipulation

E

ern

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 !
 
E

ern

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 ?
 
V

Vladimir S. Oka

ern said:
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
 
V

Vladimir S. Oka

ern said:
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
 
V

Vladimir S. Oka

ern said:
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
 
V

Vladimir S. Oka

Vladimir said:
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
 
E

ern

Vladimir said:
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 ?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top