fixed width format

G

gokhalen

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
 
W

Walter Roberson

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

Malcolm McLean

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

Walter Roberson

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')
 
C

CBFalconer

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

karthikbalaguru

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
 
B

Barry Schwarz

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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top