fprintf format

G

Gary Wessle

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
 
V

Victor Bazarov

Gary said:
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
 
G

Gary Wessle

Victor Bazarov said:
Gary said:
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


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.
 
M

Marcus Kwok

Gary Wessle said:
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.
 
M

mlimber

Gary said:
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
 
G

Gary Wessle

Gary Wessle said:
Victor Bazarov said:
Gary said:
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


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();
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top