What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Copying part of a string

Copying part of a string
Author: Message:
Chancer
Senior Member
****

Avatar

Posts: 648
Reputation: 7
34 / Male / Flag
Joined: May 2005
Status: Away
O.P. Copying part of a string
I'm making a (rudimentary) chat system at college (server + client), and it works like this:

The user (using the client app) sends a string in the format COMMAND:text, ie, "USER:Chancer" changes my name to Chancer.

The problem is, it's one single string, and I need to read the text parameter alone.
I'm doing this:
C code:
for (k = 5; buffer[k - 1] != '\0'; k++)
    text[k - 5] = buffer[k];

buffer is the string sent by the user, k starts at 5 because all available commands are in the 4-char - colon pattern.
It's partially working, but I'm afraid the text vector is not receiving the '\0' character.

Previoulsy I had this, and it was OK:
C code:
for (k = 5; buffer[k] != '\0'; k++)
    text[k - 5] = buffer[k];
text[k - 6] = '\0';


But I just think it doesn't look good. I know that using "%.5s", for example, I can print 5 characters of a string. Is there anyway to print "from character 5 until the end (\0)"?

Edit:
Cool. I found a command "sscanf" which can split a string in many different strings in any pattern you choose.

This post was edited on 05-15-2011 at 05:06 PM by Chancer.
05-14-2011 08:46 PM
Profile E-Mail PM Find Quote Report
Chancer
Senior Member
****

Avatar

Posts: 648
Reputation: 7
34 / Male / Flag
Joined: May 2005
Status: Away
O.P. RE: Copying part of a string
Damn... sscanf is not working as I imagined. I can get the command string, but only the first word of the parameter, if it has spaces.

I also tried
code:
sprintf (msg, "%s", &buffer[5]);
but it also has problems with the \0

Ideas?
05-15-2011 05:10 PM
Profile E-Mail PM Find Quote Report
Adeptus
Senior Member
****


Posts: 732
Reputation: 40
Joined: Oct 2005
RE: Copying part of a string
You probably have solved this by now, but a standard C library function you might want to take a look at, even if you did it another, way is strtok().
06-08-2011 05:49 AM
Profile E-Mail PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On