Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > stream input

Reply
Thread Tools

stream input

 
 
Old Wolf
Guest
Posts: n/a
 
      08-25-2004
"Aleks Dubinskiy" <(E-Mail Removed)> wrote:
> Hi folks,
> I am well aware of the superiority of using streams and operators << / >>
> instead of the old-style printf and scanf. However, there is only one
> obstacle that prevents me from switching to the better form.
> scanf has the following really convenient syntax:
> fscanf(pFile, "prfl num: %d\n", &nPrflNum);
> It seems to be very difficult to replace this with a convenient input stream
> version, such as:
> input>>"prfl num: ">>nPrflNum;
>
> Is there a more straightforward / painless way to do what I want?


You could use the very flexible Boost Spirit library (www.boost.org)
(warning - late-model compiler required):

#include <iostream>
#include <string>
#include "boost/spirit/core.hpp"

using namespace boost::spirit;

int main()
{
std::string s;
int i;

getline(std::cin, s);

if (parse(s.c_str(), "prfl num:" >> int_p[assign_a(i)], space_p).full)
std::cout << "input was: " << i << "\n";
else
std::cout << "input did not match expected form.\n";

return 0;
}

"int_p" indicates to parse a signed int, "[assign_a(i)]" means to
assign the result of parsing to the variable "i", "space_p"
indicates that whitespace is used to separate tokens in the input,
"full" means that the entire input was parsed correctly.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conversion from Input Stream to Output Stream Kashif Ur Rehman Java 2 05-17-2007 07:50 PM
what is the different between byte stream and character stream? dolphin Java 6 03-18-2007 01:58 PM
get stream mode flags from an opened stream Alexander Korsunsky C++ 1 02-17-2007 10:38 AM
How to GET multi-word input from a *file* stream as opposed to a *console* stream? sherifffruitfly@gmail.com C++ 9 04-27-2006 04:14 PM
Doing readline in a thread from a popen4('rsync ...') stream blocks when the stream ends. Rasmusson, Lars Python 1 04-30-2004 08:10 AM



Advertisments