Connecting Tech Pros Worldwide Help | Site Map

how to replace sscanf in C++,using <string>s

 
LinkBack Thread Tools Search this Thread
  #1  
Old August 19th, 2005, 07:25 PM
Mark
Guest
 
Posts: n/a
Default how to replace sscanf in C++,using <string>s

I want to replace the following line:

sscanf(mybuf,"%s=%s\n",sz1,sz2);

with something that produces the same effect, only with
dynamic storage i.e string s which is safer.

I might try this:

sscanf(mybuf.c_str(),"%s=%s\n",string1.c_str(),str ing2.c_str());

but unsuprisingly this causes an error at runtime since there is nothing to
stop a memory overrun happening.Additionally there is no compile time check
as sscanf uses varargs.

Can anyone recommend the quickest way of acheiving this, short of writing
my own parser.

Mark



  #2  
Old August 19th, 2005, 07:45 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: how to replace sscanf in C++,using <string>s

Mark wrote:[color=blue]
> I want to replace the following line:
>
> sscanf(mybuf,"%s=%s\n",sz1,sz2);
>
> with something that produces the same effect, only with
> dynamic storage i.e string s which is safer.
>
> I might try this:
>
> sscanf(mybuf.c_str(),"%s=%s\n",string1.c_str(),str ing2.c_str());
>
> but unsuprisingly this causes an error at runtime since there is nothing to
> stop a memory overrun happening.Additionally there is no compile time check
> as sscanf uses varargs.
>
> Can anyone recommend the quickest way of acheiving this, short of writing
> my own parser.[/color]

Something similar to

string::size_type eq = mybuf.find('=');
string string1 = mybuf.substr(0, eq), string2 = mybuf.substr(eq+1);

V
 

Bookmarks

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

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

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.