473,547 Members | 2,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fprintf format


Hi

I am trying to pretty print to file 3 vectors (int,double,dou ble)
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[i], vect2[i], vect3[i])
}}

thanks for helping

Aug 4 '06 #1
5 4100
Gary Wessle wrote:
I am trying to pretty print to file 3 vectors (int,double,dou ble)
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[i], vect2[i], vect3[i])
}}

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
Aug 4 '06 #2
"Victor Bazarov" <v.********@com Acast.netwrites :
Gary Wessle wrote:
I am trying to pretty print to file 3 vectors (int,double,dou ble)
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[i], vect2[i], vect3[i])
}}

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[i];
fmter % vect2[i];
fmter % vect3[i];
exp << fmter.str();
}

I came to this after reading as much as I can understand from the
manual.
Aug 4 '06 #3
Gary Wessle <ph****@yahoo.c omwrote:
I am trying to pretty print to file 3 vectors (int,double,dou ble)
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[i], vect2[i], vect3[i])
}}
Look into using the facilities provided by the <iomanipheade r.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Aug 4 '06 #4
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[i];
fmter % vect2[i];
fmter % vect3[i];
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

Aug 4 '06 #5
Gary Wessle <ph****@yahoo.c omwrites:
"Victor Bazarov" <v.********@com Acast.netwrites :
Gary Wessle wrote:
I am trying to pretty print to file 3 vectors (int,double,dou ble)
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[i], vect2[i], vect3[i])
}}
>
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[i];
fmter % vect2[i];
fmter % vect3[i];
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();
Aug 4 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1616
by: joy_julia446 | last post by:
Hi there, I tried to write to an output file like: Hello USER_NAME You are visitor no:COUNT USER_NAME : string and can be of any length bt 1 and 30. You: The char 'Y' has fixed at column 55. How to do this using fprintf() in making sure 'Y' is always at column 55 in this situation?
6
3481
by: hpy_awad | last post by:
I am writing stings ((*cust).name),((*cust).address)to a file using fgets but rabish is being wrote to that file ? Look to my source please and help me finding the reason why this rabish is being written. /* Book name : File name : E:\programs\cpp\iti01\ch10\ex09_5p1.cpp Program discription: Adding name,Address to...
3
3150
by: Andrew Fabbro | last post by:
I have code with stuff like this all over it: sprintf(errmsg,"somefunc(): %s has illegal character %c",somestring,somechar); fatal_error(errmsg); where fatal_error() just fprintf's to stderr and exits. I'd like to change the above to something like: fatal_error("somefunc(): %s has illegal character
6
2987
by: Magix | last post by:
Hi, I want to use fprintf to write to a file. My question about the formatted output How can I format so that I can allocate certain width for each %s (Left-aignlied) ? Example: fprintf("%s %s %s %s, name, age, city, status); <-------------------><----------------><----------------><------------->
9
3878
by: Harry | last post by:
Good Day, Is there a way to print 8 bytes in a line in a file using fprintf...like this 12 76 89 76 86 34 98 08 A4..............................
4
2707
by: grimrob | last post by:
fprintf just is not working. I am using PHP Version 4.3.9. Whatever I do fprintf just fails. For example: fwrite($Handle, 'A1'); fprintf($Handle, 'A2); The first line always works, the second always fails. Likewise any other example.
16
6665
by: Prayag Narula | last post by:
Hi, I want to redefine fprintf for debugging purposes. That is I want that all the output that is going to the stdout should be logged in a file. I tried something like #define fprintf (x,y,z) my_fprintf(x,y,z)
11
4264
by: David Mathog | last post by:
In the beginning (Kernighan & Ritchie 1978) there was fprintf, and unix write, but no fwrite. That is, no portable C method for writing binary data, only system calls which were OS specific. At C89 fwrite/fread were added to the C standard to allow portable binary IO to files. I wonder though why the choice was made to extend the unix...
4
8065
by: spiralfire | last post by:
I wrote a translator, that reads a DIMACS graph format and writes to a simpler format... basically DIMACS format is: c comment p type nodes edges //type is alwats edge on my problems, nodes is the number of nodes and edges number of edges e v1 v2 //this means an edge connecting v1 and v2, both integers
0
7510
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7437
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7703
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7797
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6032
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3493
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1050
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.