473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

String manipulation

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 !

Jan 19 '06 #1
9 1650
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 ?

Jan 19 '06 #2
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.
Jan 19 '06 #3
ern

Vladimir S. Oka wrote:


Try using "\n" as the second parameter.

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

Jan 19 '06 #4
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.
Jan 19 '06 #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.
Jan 19 '06 #6
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.
Jan 19 '06 #7
ern

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 ?

Jan 20 '06 #8
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.
Jan 20 '06 #9
ern

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.

Jan 20 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dim | last post by:
I found that C# has some buggy ways to process string across methods. I have a class with on global string var and a method where i add / remove from this string Consider it a buffer... with some...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
10
by: micklee74 | last post by:
hi if i have a some lines like this a ) "here is first string" b ) "here is string2" c ) "here is string3" When i specify i only want to print the lines that contains "string" ie the first...
5
by: Niyazi | last post by:
Hi, Does anyone knows any good code for string manipulation similar to RegularExpresion? I might get a value as string in a different format. Example: 20/02/2006 or 20,02,2006 or ...
3
by: crprajan | last post by:
String Manipulation: Given a string like “This is a string”, I want to remove all single characters( alphabets and numerals) like (a, b, 1, 2, .. ) . So the output of the string will be “This is...
7
Frinavale
by: Frinavale | last post by:
I currently have a .NET application that has an object which passes a string (a connection string) as a parameter to another object that does database manipulation. This string isn't stored...
3
by: frankeljw | last post by:
I have 2 Java strings 1st String is a series of names, colons, and numbers ie) Name1:13:Name2:4526:Name3:789:Name4:3729:Name5:6:Name6:44 2nd String is a name ie) Name2 I need to get the...
22
by: mann_mathann | last post by:
can anyone tell me a solution: i cannot use the features in standard c++ string classgh i included the string.h file but still its not working.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.