Connecting Tech Pros Worldwide Help | Site Map

Reading a tag and writing to std::cout.

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 12:51 AM
Jason Heyes
Guest
 
Posts: n/a
Default Reading a tag and writing to std::cout.

I have a function that reads tags. The function prints any failures it
encounters to std::cout.

std::istream &read_tag(std::istream &is, std::string &tag)
{
std::string s;
if (!(is >> s))
{
std::cout << "Failed to read string of tag" << std::endl;
return is;
}

if (!(s == "start" || s == "end"))
{
std::cout << "Failed to read string of tag" << std::endl;
is.setstate(std::ios::failbit);
return is;
}

tag = s;
return is;
}

How do I write a silent version of read_tag that uses read_tag to complete
its task?

std::istream &read_tag_silent(std::istream &is, std::string &tag)
{
/* ? */
}

Any help is appreciated.



  #2  
Old July 23rd, 2005, 12:51 AM
Jonathan Turkanis
Guest
 
Posts: n/a
Default Re: Reading a tag and writing to std::cout.

Noah Roberts wrote:[color=blue]
> Jason Heyes wrote:
>[color=green]
>> How do I write a silent version of read_tag that uses read_tag to
>> complete its task?[/color]
>
> I don't believe you can. Since cout is hard coded into your function
> there is no way to alter it without actually closing cout,[/color]

There's no such thing as 'closing cout'
[color=blue]
> but that
> might cause some sort of error. You should have implemented it with a
> ostream parameter for error output. Then you could pass it a bogus
> ostream that doesn't do anything with its input.[/color]

You can temporarily set cout's stream buffer to a 'null' buffer which discard
it's output.

Jonathan


  #3  
Old July 23rd, 2005, 12:51 AM
Ioannis Vranos
Guest
 
Posts: n/a
Default Re: Reading a tag and writing to std::cout.

Jason Heyes wrote:
[color=blue]
> I have a function that reads tags. The function prints any failures it
> encounters to std::cout.
>
> std::istream &read_tag(std::istream &is, std::string &tag)
> {
> std::string s;
> if (!(is >> s))
> {
> std::cout << "Failed to read string of tag" << std::endl;
> return is;
> }
>
> if (!(s == "start" || s == "end"))
> {
> std::cout << "Failed to read string of tag" << std::endl;
> is.setstate(std::ios::failbit);
> return is;
> }
>
> tag = s;
> return is;
> }
>
> How do I write a silent version of read_tag that uses read_tag to complete
> its task?
>
> std::istream &read_tag_silent(std::istream &is, std::string &tag)
> {
> /* ? */
> }[/color]


Inherit from istream, define a new operator << and create a new object
jasonCout.




--
Ioannis Vranos

http://www23.brinkster.com/noicys
  #4  
Old July 23rd, 2005, 12:51 AM
Ioannis Vranos
Guest
 
Posts: n/a
Default Re: Reading a tag and writing to std::cout.

Ioannis Vranos wrote:
[color=blue]
> Inherit from istream, define a new operator << and create a new object[/color]

jasonCin.




--
Ioannis Vranos

http://www23.brinkster.com/noicys
 

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.