473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Simple fprintf question

I have a program that expects its input in a specific format.
Mainly, it expects floating-point values to be formatted in the form:

1.32
1.
3.
3.2345

In case you missed it, all floating point values have a period, even when they
are round numbers. This is a somewhat similar behavior to the %g flag of
printf, but %g always strips the period from round numbers.
Using %#f or %#g is also no good as it just fills in trailing zeros.
I seem to be unable to force fprintf() to print out in such a way, but I'm sure
I am missing something obvious. Any ideas?

Nov 14 '05 #1
4 1486
On 02 Aug 2004 05:06:56 GMT, gg********@aol.com (GGarramuno) wrote:
I have a program that expects its input in a specific format.
Mainly, it expects floating-point values to be formatted in the form:

1.32
1.
3.
3.2345

In case you missed it, all floating point values have a period, even when they
are round numbers. This is a somewhat similar behavior to the %g flag of
printf, but %g always strips the period from round numbers.
Using %#f or %#g is also no good as it just fills in trailing zeros.
I seem to be unable to force fprintf() to print out in such a way, but I'm sure
I am missing something obvious. Any ideas?


You could use sprintf and then adjust the string to suit your needs.
<<Remove the del for email>>
Nov 14 '05 #2
GGarramuno wrote on 02/08/04 :
I have a program that expects its input in a specific format.
Mainly, it expects floating-point values to be formatted in the form:

1.32
1.
3.
3.2345
This is a special formatting. I'm not sure you can avoid to code it
yourself.
In case you missed it, all floating point values have a period, even when
they are round numbers.
Floating points are rarely 'round numbers'. There are mainly
approximations.
This is a somewhat similar behavior to the %g flag of
printf, but %g always strips the period from round numbers.
Using %#f or %#g is also no good as it just fills in trailing zeros.
I seem to be unable to force fprintf() to print out in such a way, but I'm
sure I am missing something obvious. Any ideas?


Yes, what's wrong with "%.4f"

1.3200
1.0000
3.0000
3.2345

A space problem?

If so, you have to use it with sscanf() to prepare the string, and to
'manually' trim the trailing zeros.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #3
GGarramuno wrote:
I have a program that expects its input in a specific format.
Mainly, it expects floating-point values to be formatted in the form:

1.32
1.
3.
3.2345

In case you missed it, all floating point values have a period, even when they
are round numbers. This is a somewhat similar behavior to the %g flag of
printf, but %g always strips the period from round numbers.
Using %#f or %#g is also no good as it just fills in trailing zeros.
I seem to be unable to force fprintf() to print out in such a way, but I'm sure
I am missing something obvious. Any ideas?

#include <stdio.h>
#include <string.h>

void remove_trailing_zero(char *s);

int main(void)
{
double tvals[] = { 1.32, 1., 3., 3.2345 };
size_t nvals = sizeof tvals / sizeof *tvals, i;
char buf[BUFSIZ];
for (i = 0; i < nvals; i++) {
sprintf(buf, "%f", tvals[i]);
printf("%%f -> %s\n", buf);
remove_trailing_zero(buf);
printf(" remove_trailing_zero -> %s\n", buf);
}
return 0;
}
void remove_trailing_zero(char *s)
{
char *t;
if (s && *s && strchr(s, '.'))
for (t = s + strlen(s) - 1; *t == '0'; t--)
*t = 0;
}

%f -> 1.320000
remove_trailing_zero -> 1.32
%f -> 1.000000
remove_trailing_zero -> 1.
%f -> 3.000000
remove_trailing_zero -> 3.
%f -> 3.234500
remove_trailing_zero -> 3.2345
Nov 14 '05 #4
GGarramuno wrote:

I have a program that expects its input in a specific format.
Mainly, it expects floating-point values to be formatted in the
form:

1.32
1.
3.
3.2345

In case you missed it, all floating point values have a period,
even when they are round numbers. This is a somewhat similar
behavior to the %g flag of printf, but %g always strips the
period from round numbers. Using %#f or %#g is also no good as
it just fills in trailing zeros.


This may be of use. Compile it with:

gcc -o putflt -D TESTING putflt.c

/* ---------- FILE putflt.h ---------- */
/* Print out floating point numbers
Public Domain, by C.B. Falconer.
Attribution appreciated */

#ifndef H_putflt_h
# define H_putflt_h 1
# ifdef __cplusplus
extern "C" {
# endif

/* (putchar) is a suitable value */
typedef int (*putchr)(int ch);

/* ------------ */

/* Returns the count of columns used, or -ve for error
Calling with a dummy dest allows evaluating fields */
int putflt(double val, int sigdigs, putchr dest);

/* ------------ */

/* write the value right justified in field, do not truncate
* Negative value for field left justifies.
* return value signifies field actually used */
int putfltfld(double val, int sigdigs, int field, putchr dest);

/* ------------ */

# ifdef __cplusplus
}
# endif
#endif
/* ---------- EOF FILE putflt.h ---------- */

/* Print out floating point numbers demo, file putflt.c */
/* Public Domain, by C.B. Falconer. Attribution appreciated */

#include "putflt.h"

/* ------------ */

/* Returns the count of columns used, or -ve for error
Calling with a dummy dest allows evaluating fields */
int putflt(double val, int sigdigs, putchr dest)
{
int dp;
int ch;
int used;

used = 0;
if (sigdigs <= 0) sigdigs = 1;

if (val < 0) {
dest('-'); used++;
val = - val;
}
/* Normalize */
dp = 0;
if (val != 0.0) {
while (val >= 10.0) {
val = val / 10.0; dp++;
}
while (val < 1.0) {
val = 10.0 * val; dp--;
}
}
/* Handle leading zeroes */
if (dp < 0) {
dest('0'); dest('.'); used += 2;
while (++dp) {
dest('0'); used++;
}
dp--;
}
/* The real work */
do {
ch = val; val = val - ch;
dest(ch + '0'); used++;
val = 10.0 * val;
if (0 == dp--) {
dest('.'); used++;
}
} while (--sigdigs > 0);

/* handle trailing zeroes */
while (dp >= 0) {
dest('0'); used++;
if (0 == dp--) {
dest('.'); used++;
}
}
/* Handle isolated '.' at end */
if (-1 == dp) {
dest('0'); used++;
}
return used; /* no errors handled yet */
} /* putflt */

/* ------------ */

/* do nothing, to allow evaluating field needed */
static int dummy(int ch)
{
return ch;
} /* dummy */

/* ------------ */

/* write the value right justified in field, do not truncate
* Negative value for field left justifies.
* return value signifies field actually used */
int putfltfld(double val, int sigdigs, int field, putchr dest)
{
int needed;

needed = putflt(val, sigdigs, dummy);
if (field > 0)
while (needed < field) {
dest(' '); needed++;
}
putflt(val, sigdigs, dest);
if (field < 0)
while ((needed + field) < 0) {
dest(' '); needed++;
}
return needed;
} /* putfltfld */

/* ------------ */

#ifdef TESTING

#include <stdio.h>

#define show(fld) printf(" Field = %d\n", fld)

int main(void)
{
double val;
int i, fld;

val = 10.0 / 7.0; /* A normalized value, 6 dig rep */

fld = putflt(-val / 100, 10, (putchar)); show(fld);
fld = putflt(-val, 10, (putchar)); show(fld);
fld = putflt(-val * 100, 10, (putchar)); show(fld);
fld = putflt(val / 100, 10, (putchar)); show(fld);
fld = putflt(val, 10, (putchar)); show(fld);
for (i = 12; i > 0; i--) {
val = 10 * val;
fld = putflt(val, 10, (putchar)); show(fld);
}
fld = putflt(0.0, 10, (putchar)); show(fld);
fld = putflt(1e-30, 10, (putchar)); show(fld);
fld = putflt(1e30, 10, (putchar)); show(fld);
fld = putflt(0.1e-30, 10, (putchar)); show(fld);
fld = putflt(0.2e-30, 10, (putchar)); show(fld);
fld = putflt(0.3e-30, 10, (putchar)); show(fld);
fld = putflt(0.01e-30, 10, (putchar)); show(fld);
fld = putflt(0.001e-30, 10, (putchar)); show(fld);

val = 30.0 / 13.0; /* Another normalized value */
fld = putfltfld(-val, 10, 20, (putchar)); show(fld);
fld = putfltfld(-val * 100, 10, -20, (putchar)); show(fld);
fld = putfltfld(val / 100, 10, -20, (putchar)); show(fld);
fld = putfltfld(val, 10, 20, (putchar)); show(fld);
fld = putfltfld(val, 25, 35, (putchar)); show(fld);

val = 5419351.0 / 1725033.0; /* Another normalized value */
fld = putfltfld(val, 25, 35, (putchar)); show(fld);

return 0;
} /* main putflt demo */
#endif
/* ---------- EOF FILE putflt.c ---------- */

--
"I'm a war president. I make decisions here in the Oval Office
in foreign policy matters with war on my mind." - Bush.
"Churchill and Bush can both be considered wartime leaders, just
as Secretariat and Mr Ed were both horses." - James Rhodes.
Nov 14 '05 #5

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

Similar topics

3
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...
6
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...
17
by: G Patel | last post by:
E. Robert Tisdale wrote: > > int main(int argc, char* argv) { > quad_t m = {0, 1, 2, 3}; > int r; > fprintf(stdout, "m = ("); > for (size_t...
3
by: Jim Hunter | last post by:
From what I've read, the only indication fprintf gives of an error is a negative return value. I have a series of writes to a file using fprintf, and, while I need to know if a failure has...
10
by: Brennan Young | last post by:
Hi there, First I'll apologise for my ignorance! I have been attempting to compile some code that I found on the net. AFAICT it's intended to be portable code (it was available as source and...
15
by: key9 | last post by:
Why still can't compile (simple) Hi all I have nothing to say , the code still can not be complie, where's problem? ========================================= //test.cpp #include <stdio.h>...
5
by: yojimbo2005 | last post by:
hey all, as part of my computing module i have been asked to create a program that will promp the user for the following: depth_beam /* i.e fprintf etc "enter value of beam" then fscanf %lf...
11
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...
3
by: vamsi | last post by:
Hi, I have a program, where involves creation of a thread with stack size of 16k(minimum stack size). There is an fprintf statement in the created thread code. I see that there is a core dump...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.