fprintf format

Discussion in 'C++' started by Gary Wessle, Aug 4, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    Hi

    I am trying to pretty print to file 3 vectors (int,double,double)
    where all the cells match like a table format where every thing lines
    up to the left and space padded to the right.
    does c++ has something or mostly use C fprintf?

    1255251 0.251025 215.2541
    thats how I like it to look, where the space is to the right of the
    number and the number left aligns with the field border.

    here is my patented effort

    int main() {
    sync_with_stdio(); //to mix C and C++

    ofstream out("output.txt")
    for (int i=0; i<vect.size(); i++){
    fprintf(out,
    "%d8 %f8 %f8"
    ,vect1, vect2, vect3)
    }}

    thanks for helping
     
    Gary Wessle, Aug 4, 2006
    #1
    1. Advertising

  2. Gary Wessle wrote:
    > I am trying to pretty print to file 3 vectors (int,double,double)
    > where all the cells match like a table format where every thing lines
    > up to the left and space padded to the right.
    > does c++ has something or mostly use C fprintf?
    >
    > 1255251 0.251025 215.2541
    > thats how I like it to look, where the space is to the right of the
    > number and the number left aligns with the field border.
    >
    > here is my patented effort
    >
    > int main() {
    > sync_with_stdio(); //to mix C and C++
    >
    > ofstream out("output.txt")
    > for (int i=0; i<vect.size(); i++){
    > fprintf(out,
    > "%d8 %f8 %f8"


    Any width you need should be placed between the % sign and the format
    specifier ('d' or 'f'): %8d %8f. RTFM, please.

    > ,vect1, vect2, vect3)
    > }}
    >
    > thanks for helping


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 4, 2006
    #2
    1. Advertising

  3. Gary Wessle

    Gary Wessle Guest

    "Victor Bazarov" <> writes:

    > Gary Wessle wrote:
    > > I am trying to pretty print to file 3 vectors (int,double,double)
    > > where all the cells match like a table format where every thing lines
    > > up to the left and space padded to the right.
    > > does c++ has something or mostly use C fprintf?
    > >
    > > 1255251 0.251025 215.2541
    > > thats how I like it to look, where the space is to the right of the
    > > number and the number left aligns with the field border.
    > >
    > > here is my patented effort
    > >
    > > int main() {
    > > sync_with_stdio(); //to mix C and C++
    > >
    > > ofstream out("output.txt")
    > > for (int i=0; i<vect.size(); i++){
    > > fprintf(out,
    > > "%d8 %f8 %f8"

    >
    > Any width you need should be placed between the % sign and the format
    > specifier ('d' or 'f'): %8d %8f. RTFM, please.
    >
    > > ,vect1, vect2, vect3)
    > > }}
    > >
    > > thanks for helping

    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    after all kind of trial and error, I wish I could find some
    step-by-step gentle into to the fprintf with lots of examples for
    the dummy.

    but here it is for the records, in c++ and not c

    ****************************************************************
    #include "boost/format.hpp"
    using boost::format;

    ofstream out("output.txt");
    format fmter("%1% %12t%2% %25t%3%\n");//very close to fprintf format

    for(unsigned i=0; i<vect1.size(); i++){
    fmter % vect1;
    fmter % vect2;
    fmter % vect3;
    exp << fmter.str();
    }

    I came to this after reading as much as I can understand from the
    manual.
     
    Gary Wessle, Aug 4, 2006
    #3
  4. Gary Wessle

    Marcus Kwok Guest

    Gary Wessle <> wrote:
    > I am trying to pretty print to file 3 vectors (int,double,double)
    > where all the cells match like a table format where every thing lines
    > up to the left and space padded to the right.
    > does c++ has something or mostly use C fprintf?
    >
    > 1255251 0.251025 215.2541
    > thats how I like it to look, where the space is to the right of the
    > number and the number left aligns with the field border.
    >
    > here is my patented effort
    >
    > int main() {
    > sync_with_stdio(); //to mix C and C++
    >
    > ofstream out("output.txt")
    > for (int i=0; i<vect.size(); i++){
    > fprintf(out,
    > "%d8 %f8 %f8"
    > ,vect1, vect2, vect3)
    > }}


    Look into using the facilities provided by the <iomanip> header.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Aug 4, 2006
    #4
  5. Gary Wessle

    mlimber Guest

    Gary Wessle wrote:
    > after all kind of trial and error, I wish I could find some
    > step-by-step gentle into to the fprintf with lots of examples for
    > the dummy.
    >
    > but here it is for the records, in c++ and not c
    >
    > ****************************************************************
    > #include "boost/format.hpp"
    > using boost::format;
    >
    > ofstream out("output.txt");
    > format fmter("%1% %12t%2% %25t%3%\n");//very close to fprintf format
    >
    > for(unsigned i=0; i<vect1.size(); i++){
    > fmter % vect1;
    > fmter % vect2;
    > fmter % vect3;
    > exp << fmter.str();
    > }
    >
    > I came to this after reading as much as I can understand from the
    > manual.


    Why not use std::cout instead? It's standard (unlike Boost), type-safe,
    and is preferred in C++ land. Consult your C++ reference (if you don't
    have one, get one recommended at accu.org) on how to use the items in
    the iomanip header to achieve the same effect in a more idiomatic C++
    way.

    Cheers! --M
     
    mlimber, Aug 4, 2006
    #5
  6. Gary Wessle

    Gary Wessle Guest

    Gary Wessle <> writes:

    > "Victor Bazarov" <> writes:
    >
    > > Gary Wessle wrote:
    > > > I am trying to pretty print to file 3 vectors (int,double,double)
    > > > where all the cells match like a table format where every thing lines
    > > > up to the left and space padded to the right.
    > > > does c++ has something or mostly use C fprintf?
    > > >
    > > > 1255251 0.251025 215.2541
    > > > thats how I like it to look, where the space is to the right of the
    > > > number and the number left aligns with the field border.
    > > >
    > > > here is my patented effort
    > > >
    > > > int main() {
    > > > sync_with_stdio(); //to mix C and C++
    > > >
    > > > ofstream out("output.txt")
    > > > for (int i=0; i<vect.size(); i++){
    > > > fprintf(out,
    > > > "%d8 %f8 %f8"

    > >
    > > Any width you need should be placed between the % sign and the format
    > > specifier ('d' or 'f'): %8d %8f. RTFM, please.
    > >
    > > > ,vect1, vect2, vect3)
    > > > }}
    > > >
    > > > thanks for helping

    > >
    > > V
    > > --
    > > Please remove capital 'A's when replying by e-mail
    > > I do not respond to top-posted replies, please don't ask

    >
    > after all kind of trial and error, I wish I could find some
    > step-by-step gentle into to the fprintf with lots of examples for
    > the dummy.
    >
    > but here it is for the records, in c++ and not c
    >
    > ****************************************************************
    > #include "boost/format.hpp"
    > using boost::format;
    >
    > ofstream out("output.txt");
    > format fmter("%1% %12t%2% %25t%3%\n");//very close to fprintf format
    >
    > for(unsigned i=0; i<vect1.size(); i++){
    > fmter % vect1;
    > fmter % vect2;
    > fmter % vect3;
    > exp << fmter.str();
    > }
    >
    > I came to this after reading as much as I can understand from the
    > manual.

    correction
    the last line of the above code should be
    out << fmter.str();
     
    Gary Wessle, Aug 4, 2006
    #6
    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. Arti Potnis
    Replies:
    2
    Views:
    6,751
    Howard
    Sep 2, 2004
  2. Koen
    Replies:
    1
    Views:
    593
    Victor Bazarov
    Oct 5, 2004
  3. Jos De Laender

    My error or fprintf implementation error ?

    Jos De Laender, Aug 14, 2003, in forum: C Programming
    Replies:
    7
    Views:
    535
    CBFalconer
    Aug 15, 2003
  4. google
    Replies:
    5
    Views:
    5,178
    Dan Pop
    Apr 13, 2004
  5. Harry

    fprintf format specifiers

    Harry, Oct 12, 2006, in forum: C Programming
    Replies:
    9
    Views:
    479
    Christopher Benson-Manica
    Oct 13, 2006
Loading...

Share This Page