Connecting Tech Pros Worldwide Help | Site Map

Reading a tag and writing to std::cout.

Jason Heyes
Guest
 
Posts: n/a
#1: Jul 23 '05
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.


Jonathan Turkanis
Guest
 
Posts: n/a
#2: Jul 23 '05

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


Ioannis Vranos
Guest
 
Posts: n/a
#3: Jul 23 '05

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
Ioannis Vranos
Guest
 
Posts: n/a
#4: Jul 23 '05

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