By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,953 Members | 1,547 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,953 IT Pros & Developers. It's quick & easy.

fixed width format

P: n/a
How can I print between column number 8 and column number 80?. Is
there a format specifier that will do it easily?. I am printing an
array of doubles and I want the number to be printed on the current
line only if it ends before 80. If not it should be printed on the
next line, starting at column number 6.

Best,

-Nachiket

Sep 6 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
In article <11**********************@k79g2000hse.googlegroups .com>,
<go******@gmail.comwrote:
>How can I print between column number 8 and column number 80?. Is
there a format specifier that will do it easily?. I am printing an
array of doubles and I want the number to be printed on the current
line only if it ends before 80. If not it should be printed on the
next line, starting at column number 6.
There is no format specifier for that.

If you are using C99, or if you are using C89 and your implementation
supports a common C89 extension that became standard in C99, then
you can use snprint(). If you pass snprint a NULL string to
print into, it will tell you how many characters would have been
needed for the printing, but without storing or printing anything.
You can look at the character count to decide how much to
actually output.

Alternately, if you use a fixed-width format, you can predict
at code creation time how many characters will be needed for each field,
and you would not need to adjust anything on the fly... provided,
of course, that there isn't anything on the line that forces you
to print variable-width, and provided that it is acceptable to
line up the printing into columns instead of printing the minimum
needed per column.
--
Programming is what happens while you're busy making other plans.
Sep 6 '07 #2

P: n/a

<go******@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
How can I print between column number 8 and column number 80?. Is
there a format specifier that will do it easily?. I am printing an
array of doubles and I want the number to be printed on the current
line only if it ends before 80. If not it should be printed on the
next line, starting at column number 6.

Best,

-Nachiket
You can't do this easily.
You can write a function

int doublefmt(char *buff, double x, FILE *fp)
{
char temp[64];
if(strlen(buff) == 0)
strcpy(buff, " ");
sprintf(temp, "%g ", x);
if(strlen(buff) + strlen(temp) < 80)
strcat(buff, temp);
else
{
fprintf(fp, "%s\n", buff);
strcpy(buff, "");
}
}

(untested code).

You will need to set up buff as the empty string, and as a buffer of 81
chars or greater. You will also need to flush out the last remainign double
in the buffer. You might alos want a bit more fiddling with the terminal
space.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Sep 6 '07 #3

P: n/a
In article <fb**********@canopus.cc.umanitoba.ca>,
Walter Roberson <ro******@ibd.nrc-cnrc.gc.cawrote:
>If you are using C99, or if you are using C89 and your implementation
supports a common C89 extension that became standard in C99, then
you can use snprint().
Correct, snprintf() (missed the 'f')
--
Programming is what happens while you're busy making other plans.
Sep 6 '07 #4

P: n/a
go******@gmail.com wrote:
>
How can I print between column number 8 and column number 80?. Is
there a format specifier that will do it easily?. I am printing an
array of doubles and I want the number to be printed on the current
line only if it ends before 80. If not it should be printed on the
next line, starting at column number 6.
Start by noticing that printf returns the number of chars output.
Worry about tabs.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Sep 7 '07 #5

P: n/a
On Sep 7, 1:34 am, gokha...@gmail.com wrote:
How can I print between column number 8 and column number 80?. Is
there a format specifier that will do it easily?. I am printing an
array of doubles and I want the number to be printed on the current
line only if it ends before 80. If not it should be printed on the
next line, starting at column number 6.

Best,

-Nachiket
You can use sprintf . But, it seems that sprintf has buffer overrun
problems.
So, better stick with snprintf.

Thx,
Karthik Balaguru

Sep 7 '07 #6

P: n/a
On Thu, 6 Sep 2007 20:57:40 +0000 (UTC), ro******@ibd.nrc-cnrc.gc.ca
(Walter Roberson) wrote:
>In article <11**********************@k79g2000hse.googlegroups .com>,
<go******@gmail.comwrote:
>>How can I print between column number 8 and column number 80?. Is
there a format specifier that will do it easily?. I am printing an
array of doubles and I want the number to be printed on the current
line only if it ends before 80. If not it should be printed on the
next line, starting at column number 6.

There is no format specifier for that.

If you are using C99, or if you are using C89 and your implementation
supports a common C89 extension that became standard in C99, then
you can use snprint(). If you pass snprint a NULL string to
print into, it will tell you how many characters would have been
needed for the printing, but without storing or printing anything.
You can look at the character count to decide how much to
actually output.
If your system does not have snprint, you can use sprintf to a work
buffer. The return value is the character count.
>
Alternately, if you use a fixed-width format, you can predict
at code creation time how many characters will be needed for each field,
and you would not need to adjust anything on the fly... provided,
of course, that there isn't anything on the line that forces you
to print variable-width, and provided that it is acceptable to
line up the printing into columns instead of printing the minimum
needed per column.

Remove del for email
Sep 7 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.