write to output file Query

Discussion in 'C++' started by joy_julia446@yahoo.com, Sep 6, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. <> wrote in message
    news:...
    > 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
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Sep 7, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joel Finkel
    Replies:
    1
    Views:
    3,546
    Joel Finkel
    Aug 15, 2003
  2. G
    Replies:
    0
    Views:
    402
  3. Replies:
    2
    Views:
    257
    Karl Heinz Buchegger
    Sep 1, 2005
  4. Iulian Ilea
    Replies:
    1
    Views:
    308
    pcx99
    Dec 21, 2006
  5. Tim Chase
    Replies:
    0
    Views:
    88
    Tim Chase
    Dec 16, 2013
Loading...

Share This Page