write to output file Query

J

joy_julia446

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
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top