By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,617 Members | 1,708 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,617 IT Pros & Developers. It's quick & easy.

write to output file Query

P: n/a
Hi there,
I would like to print my result as below in an output file. I would
appreciate if someone can give me some advice.

RESULT
ClassA,96.678,88.196,8.048,-0.233,456.231,5.890, 1
11.371,-0.906,-0.025,9999.888,76.888,0.001,3456.899 2
20.422,-20.979,0.422,4567.987,987.987,0.985,0.445, 3
-0.044,0.905,1.335,890.987,34.567,32.235,76.874; 4
let say the results are stored in an arry:
a[0] = 96.678, a[1] = 88.196, a[2] = 8.048, a[3] = -0.233, a[4] =
11.371, a[5] = -0.906, .........a[26] = 76.874.

The max column for each line that I can print is 51. Once it reach 51,
I need to start the next row.

The figure on the far right side ie. 1 2 3 and 4 represents the row
number and have to be printed at column 54th.

My query:
1.How am I suppose to keep track the number of row that I have printed?
for example, I have used 48columns in row1. Since 11.371 occupies
6spaces, I need to start the new line.

2. How do fix the row number at column 54th?

Please kindly note that the size of the array can be as large as
thousand. In this case, I try to simplify my problem here.

Thank you

Sep 6 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
<jo**********@yahoo.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi there,
I would like to print my result as below in an output file. I would
appreciate if someone can give me some advice.

RESULT
ClassA,96.678,88.196,8.048,-0.233,456.231,5.890, 1
11.371,-0.906,-0.025,9999.888,76.888,0.001,3456.899 2
20.422,-20.979,0.422,4567.987,987.987,0.985,0.445, 3
-0.044,0.905,1.335,890.987,34.567,32.235,76.874; 4
let say the results are stored in an arry:
a[0] = 96.678, a[1] = 88.196, a[2] = 8.048, a[3] = -0.233, a[4] =
11.371, a[5] = -0.906, .........a[26] = 76.874.

The max column for each line that I can print is 51. Once it reach 51,
I need to start the next row.

The figure on the far right side ie. 1 2 3 and 4 represents the row
number and have to be printed at column 54th.

My query:
1.How am I suppose to keep track the number of row that I have printed?
for example, I have used 48columns in row1. Since 11.371 occupies
6spaces, I need to start the new line.
Keep the current line in std::string format and check whether the new part's
addition will wrap the line. A templatized member function should work fine:
class Line
{
/* ... */

// The constructor can take the actual output stream and the line length.
// You could also get the delimiter here...
Line(ostream & output, size_t line_length);

template <class T>
void add(T const & object)
{
string const new_part = as_string(object);

if (line_.size() + new_part.size() + 1 > line_length_)
{
/* TODO: output the current line here */

line_ = new_part;
}
else
{
line_ += " ";
line_ += new_part;
}
}
};

You can use boost::lexical_cast, or this poor function for as_string:

// Prefer boost::lexical_cast to this crippled function
template <class T>
string as_string(T const & object)
{
ostringstream stream;
stream << object;
return stream.str();
}

You could then use Line as:

Line line(cout, 51);

line.add("ClassA");
line.add(96.678);


2. How do fix the row number at column 54th?


Look at setw manipulator that is defined in <iomanip>. Basically you would
set the width of the line number, before outputting it:

your_output_stream << setw(4) << line_number_;

Ali

Sep 6 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.