Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Request help -- odd sprintf, fprintf issue |
| Date | 2016-08-29 10:27 -0700 |
| Organization | None to speak of |
| Message-ID | <ln1t17cwp8.fsf@kst-u.example.com> (permalink) |
| References | <e9765d0c-e510-41ee-9b72-02a85734d45d@googlegroups.com> |
dakupoto@gmail.com writes:
> Could some C language guru please help ?
> I am using gcc on Fedora 18.
>
> I have a simple function to write formatted
> character strings to a text file, the snippet
> that is causing problem is:
>
> memset(b, 0, SLEN);
> sprintf(b, "%s%s%d%s%d%s%f%s",
> "RS"," ",snode," ",enode," ",ld,"\n");
> fprintf(a, "%s", b);
> memset(b, 0, SLEN);
As Ben pointed out, your use of repeated adjacent "%s" format
specifies makes your code hard to read. If an argument is a string
literal, just include it in the format string -- unless this is
a simplified example of something more general. The spaces in
particular are likely to be invariant.
sprintf(b, "%s %d %d %f\n", "RS", snode, enode, ld);
or
sprintf(b, "RS %d %d %f\n", snode, enode, ld);
(I also find that a space after each comma makes code much more
legible.)
You might also consider using fputs rather than fprintf, since
you're not doing any actual formatting.
fputs(b, a);
(On the other hand, there is something to be said for using *printf
consistently rather than a mix of *printf, put, fputs, putchar,
et al).
> Here, 'a' is the non-NULL pointer to a text file,
> 'b' is character array of size 32, and enode and
> snode are integers as 1, 2, ...
>
> The output looks is(for one particular run):
>
> S 1 2 50.00
>
> I do not understand what is causing the 'R' in
> 'RS' to not get printed
You say that's what the output *looks like*. Are you sure that's
what the output *is*? Is it possible that the "RS" is actually
being written, but is being overwritten on your screen due to
control characters?
View the output file with some tool that shows its contents
unambiguously, such as `cat -A` or `od -c`.
> Another identical set of statements some lines
> later in the same function work flawlessly.
>
> Any hints, suggestions would be of immense
> help. Thanks in advance.
Can you create a small self-contained test program that reproduces
the issue? The process of doing so will often let you solve the problem
yourself, and even if it doesn't the rest of us are more likely to be
able to diagnose it.
I just tried copying your code into a complete program, and I got the
expected output -- which suggests rather strongly that the problem is in
something you haven't shown us. Since you don't know what the problem
is, you (almost by definition) don't know what information you can
safely omit -- which makes a small self-contained example even more
important.
One of the variables whose value you print is named "ld" and you
print it with a %f" format. If it's of type float or double with
the value 50.0, it will be printed as "50.000000" -- but the output
you report is "50.00", which indicates that the code you've shown us
doesn't correspond to the output you've shown us. If, as the name
"ld" suggests, it's of type long double, then your sprintf call
has undefined behavior because you're using the wrong format string
(use "%f" for float or double, "%Lf" for long double).
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Request help -- odd sprintf, fprintf issue dakupoto@gmail.com - 2016-08-28 23:58 -0700
Re: Request help -- odd sprintf, fprintf issue gazelle@shell.xmission.com (Kenny McCormack) - 2016-08-29 08:09 +0000
Re: Request help -- odd sprintf, fprintf issue Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-08-29 10:30 +0100
Re: Request help -- odd sprintf, fprintf issue BartC <bc@freeuk.com> - 2016-08-29 10:48 +0100
Re: Request help -- odd sprintf, fprintf issue Brett Dong <brett.browning.dong@gmail.com> - 2016-08-29 18:15 +0800
Re: Request help -- odd sprintf, fprintf issue Dutch Ingraham <spikeheron@att.net> - 2016-08-29 08:21 -0500
Re: Request help -- odd sprintf, fprintf issue Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2016-08-29 09:10 -0600
Re: Request help -- odd sprintf, fprintf issue Keith Thompson <kst-u@mib.org> - 2016-08-29 10:27 -0700
Re: Request help -- odd sprintf, fprintf issue Ike Naar <ike@iceland.freeshell.org> - 2016-08-29 19:51 +0000
Re: Request help -- odd sprintf, fprintf issue Keith Thompson <kst-u@mib.org> - 2016-08-29 14:30 -0700
Re: Request help -- odd sprintf, fprintf issue Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-08-30 10:56 +0100
csiph-web