how to put tab characters into ostream?

A

auditory

i am working with the program which generates
c language source code.

i implemented function adding indentation tabs to each line like

std::string tabcharcter(int depth)
{
string tab;
for(int i=0;i<depth;i++) tab+="\t";
return tab;
}

in some function
{
....
ostream os;

while(..)
{
os << tabcharcter(3);
os << string_line << endl;
}
....
}

is there any better way for this?
like some output manipulater, or,
automatic way to add n tabs after new line character added.
 
E

Earl Purple

auditory said:
i am working with the program which generates
c language source code.

i implemented function adding indentation tabs to each line like

std::string tabcharcter(int depth)
{
string tab;
for(int i=0;i<depth;i++) tab+="\t";
return tab;
}

There is no need for your tabcharacter function as you can create a
string with its constructor that takes a size and a character. Note it
takes the size first.
in some function
{
...
ostream os;

while(..)
{
os << tabcharcter(3);
os << string_line << endl;
}
...
}
is there any better way for this?
like some output manipulater, or,
automatic way to add n tabs after new line character added.

I don't know what is string_line but if you are iterating through
vector<string> then you can make use of ostream_iterator's delimiter

something in the nature of:

std::string delim( len, '\t' );

std::copy
(
lines.begin(), lines,end(),
std::eek:stream_iterator< std::string >( os, delim.c_str() )
);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top