Hi,

I am trying to write strings to a file line by line and trying to format the output so that the alignment is proper.
output should be
it should appear in the output file as

david      10   15   16 
sam        11   15   16   

but appears as david 10 15 16 sam 11 15 16 there is no new line all output is on the same line despite using endl.
my code is attached below..pls tell me thats wrong, or if some other method exists to implement the above as i want the numbers to be aligned column wise.
Note: all parameters are strings like david ,10,15,16 will all be seperate strings

bool WriteOutputFile(string line)
{
string Outline;
int linelength;

    Outline=line;
    Outline.resize(Outline.length()+20);

    ofstream Outfile ("result.txt", ios::out | ios::app );
    if(Outfile.is_open())
    {
        Outline.insert(20,"10");
        Outline.insert(25,"15");
        Outline.insert(30,"16");

        Outfile<<Outline<<endl;
    }
    Outfile.close();
}

Recommended Answers

See [url]http://www.cplusplus.com/reference/iostream/manipulators/setw/[/url]
That way the widths of your fields will be constant and you can use your ostream directly without all that mildly stressful string manipulation.

Jump to Post

All 3 Replies

Re: Writing output to a file, string does not start on new line 80 80

hi,
Did u try writing the '\n' char instead of 'endl'?

Re: Writing output to a file, string does not start on new line 80 80

See http://www.cplusplus.com/reference/iostream/manipulators/setw/
That way the widths of your fields will be constant and you can use your ostream directly without all that mildly stressful string manipulation.

commented: excellent help! +0
Re: Writing output to a file, string does not start on new line 80 80

thanks jonsca

it worked like a charm using setw is so much easier...and i got the solution of the new line issue seems cygwin recognises /r as new line command!

thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.