Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Request help -- odd sprintf, fprintf issue Date: Mon, 29 Aug 2016 10:27:15 -0700 Organization: None to speak of Lines: 89 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="24096"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/AN+wr9HZcFToIhcMP3YND" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:HOamg/46zR1+sR/vYQSwaBZO1aY= sha1:Y0d3o8nzb/fGNSFflvcyqiMRfPs= Xref: csiph.com comp.lang.c:92876 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 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"