Connecting Tech Pros Worldwide Help | Site Map

Reading a tag and writing to std::cout.

  #1  
Old July 23rd, 2005, 01:51 AM
Jason Heyes
Guest
 
Posts: n/a
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, 01:51 AM
Jonathan Turkanis
Guest
 
Posts: n/a

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, 01:51 AM
Ioannis Vranos
Guest
 
Posts: n/a

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, 01:51 AM
Ioannis Vranos
Guest
 
Posts: n/a

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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conversion between float and long d major answers 8 June 27th, 2008 05:44 PM
getNodeType() function not working Properly hepsibasushmap@infotechsw.com answers 2 March 28th, 2006 06:25 AM