Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - Parsing char array to CString

 
Thread Tools Search this Thread
Old 09-06-2003, 09:18 PM   #1
Default Parsing char array to CString


Hi all,

I have a char buff[100] which contains "1234 ABCD 5678".

I can't remember what function I used before to parse the string into
individual variables. It was something like this:

forgotenFunction(var1, var2, var3, buff, "%d %s %d")

It gave me this:

var1 = "1234"
var2 = "ABCD"
var3 = "5678"

Any ideas? I will appreciate it.

J.




John Smith
  Reply With Quote
Old 09-06-2003, 09:55 PM   #2
John Smith
 
Posts: n/a
Default Re: Parsing char array to CString

Ok, I found it. It is sscan().


"John Smith" <> wrote in message
news:bjdfeu$26p4$...
> Hi all,
>
> I have a char buff[100] which contains "1234 ABCD 5678".
>
> I can't remember what function I used before to parse the string into
> individual variables. It was something like this:
>
> forgotenFunction(var1, var2, var3, buff, "%d %s %d")
>
> It gave me this:
>
> var1 = "1234"
> var2 = "ABCD"
> var3 = "5678"
>
> Any ideas? I will appreciate it.
>
> J.
>
>



  Reply With Quote
Old 09-08-2003, 06:10 PM   #3
phykell
 
Posts: n/a
Default Re: Parsing char array to CString

int sprintf(char *buffer, const char *format [,argument] ... );

"John Smith" <> wrote in message
news:bjdfeu$26p4$...
> Hi all,
>
> I have a char buff[100] which contains "1234 ABCD 5678".
>
> I can't remember what function I used before to parse the string into
> individual variables. It was something like this:
>
> forgotenFunction(var1, var2, var3, buff, "%d %s %d")
>
> It gave me this:
>
> var1 = "1234"
> var2 = "ABCD"
> var3 = "5678"
>
> Any ideas? I will appreciate it.
>
> J.
>
>



  Reply With Quote
Old 09-08-2003, 08:38 PM   #4
Kevin Goodsell
 
Posts: n/a
Default Re: Parsing char array to CString

phykell wrote:

> int sprintf(char *buffer, const char *format [,argument] ... );


Please don't top-post. Re-read section 5 of the FAQ for posting
guidelines. You might also be interested in section 3 of RFC 1855
(Netiquette Guide):

http://www.parashift.com/c++-faq-lite/
http://www.dtcc.edu/cs/rfc1855.html

Your answer is also wrong, and wouldn't be a very good suggestion even
if it wasn't. sprintf is a very dangerous function that is very
difficult to use correctly.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

  Reply With Quote
Old 09-09-2003, 01:20 PM   #5
Pat McCormack
 
Posts: n/a
Default Re: Parsing char array to CString


"John Smith" <> wrote in message
news:bjdfeu$26p4$...
> Hi all,
>
> I have a char buff[100] which contains "1234 ABCD 5678".
>
> I can't remember what function I used before to parse the string into
> individual variables. It was something like this:
>
> forgotenFunction(var1, var2, var3, buff, "%d %s %d")
>
> It gave me this:
>
> var1 = "1234"
> var2 = "ABCD"
> var3 = "5678"
>
> Any ideas? I will appreciate it.
>
> J.
>
>


If you'd like to use C++ rather than C, you'd use something like;

#include <iostream>
#include <string>
#include <sstream>
int main(int argc, char* argv[])
{
std::string str("1234 ABCD 5678");
std::stringstream sstr(str);
int var1,var3;
std::string var2;
sstr >> var1 >> var2 >> var3;
std::cout<<var1<<"\n"<<var2<<"\n"<<var3<<"\n";

return 0;
}

It is a lot safer. However one advantage in sscanf is that it handles fixed
width numerics much more tidily. The stringstream operators have the notion
of delimiters hardwired in (which very strong coupling to the data I find
pretty weird in an OO library). The setwidth() manipulators only work if the
next extraction is to a string.

If you are doing lots of this stuff it is worth looking at the boost
libraries for <lexical_cast>
The old gcc stream class had stream::sscanf(). I think BSD still has this
implementation though I stand to be corrected.


Pat


  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump