Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #92849 > unrolled thread
| Started by | dakupoto@gmail.com |
|---|---|
| First post | 2016-08-28 23:58 -0700 |
| Last post | 2016-08-30 10:56 +0100 |
| Articles | 11 — 9 participants |
Back to article view | Back to comp.lang.c
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
| From | dakupoto@gmail.com |
|---|---|
| Date | 2016-08-28 23:58 -0700 |
| Subject | Request help -- odd sprintf, fprintf issue |
| Message-ID | <e9765d0c-e510-41ee-9b72-02a85734d45d@googlegroups.com> |
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);
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
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.
[toc] | [next] | [standalone]
| From | gazelle@shell.xmission.com (Kenny McCormack) |
|---|---|
| Date | 2016-08-29 08:09 +0000 |
| Message-ID | <nq0qk6$k9o$2@news.xmission.com> |
| In reply to | #92849 |
In article <e9765d0c-e510-41ee-9b72-02a85734d45d@googlegroups.com>,
<dakupoto@gmail.com> wrote:
>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);
What is 'ld'? Do you really want to use "%f" to print it?
Anyway, you probably want to simplify as follows - you will probably find
your bug(s) by so doing:
1) No need to sprintf() first and then fprintf() the result. Why not
just use one fprintf() call?
2) No need to "%s" out the constants (the spaces and the newline).
Just include them in the format string. That's what the printf()
family functions are for.
--
The randomly chosen signature file that would have appeared here is more than 4
lines long. As such, it violates one or more Usenet RFCs. In order to remain in
compliance with said RFCs, the actual sig can be found at the following web address:
http://www.xmission.com/~gazelle/Sigs/Aspergers
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-08-29 10:30 +0100 |
| Message-ID | <878tvfly6z.fsf@bsb.me.uk> |
| In reply to | #92849 |
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); That looks very odd (though not wrong). Is the code automatically generated from something else? If not, why is i written this way with a temporary buffer (b), the memset operations, and such a complex format? I'd have written just one line: fprintf(a, "RS %d %d %f\n", snode, encode, ld); > 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 That's very odd. Without a specified precision, %f should print 6 digits after the point so there is more going on here than meets the eye. Cutting a pasting your text into a program gives me the line RS 1 2 50.000000 > I do not understand what is causing the 'R' in > 'RS' to not get printed Nor I. The bug is almost certainly somewhere else in the program. Can you write a small self-contained version of the program that exhibits the same behaviour? Very often, you'll find the problem yourself as you are trying to prepare such an example for posting. (But if that happens, plase do post again to say it's all resolved.) <snip> -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | BartC <bc@freeuk.com> |
|---|---|
| Date | 2016-08-29 10:48 +0100 |
| Message-ID | <nq10do$dpf$1@dont-email.me> |
| In reply to | #92849 |
On 29/08/2016 07:58, dakupoto@gmail.com wrote: > 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); > > 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 What happens if you replace the fprintf with printf? What if, just before the above code, you write: fprintf(a,"<"); Do you get "<S 1 2 50.00" or "<RS 1 2 50.0"? (Or "RS 1 2 50.0" and it's the "<" that's missing.) What is the return value of sprintf? (It should be the number of characters printed, which I think is 13 if "\n" is counted as one.) Since the output is to a file, what do you use to look at the file? Do you get the same result if you look at a dump of the file? (As the "R" might somehow hidden or overwritten.) -- Bartc
[toc] | [prev] | [next] | [standalone]
| From | Brett Dong <brett.browning.dong@gmail.com> |
|---|---|
| Date | 2016-08-29 18:15 +0800 |
| Message-ID | <f9679b48-a8a8-a86a-89c9-fea42c504574@gmail.com> |
| In reply to | #92849 |
On 8/29/16 2:58 PM, dakupoto@gmail.com wrote: > > The output looks is(for one particular run): > > S 1 2 50.00 > Do you mean the outputs aren't all the same, or say, different, in multiple runs? That's feeling weird, did you use multi-threading in your program?
[toc] | [prev] | [next] | [standalone]
| From | Dutch Ingraham <spikeheron@att.net> |
|---|---|
| Date | 2016-08-29 08:21 -0500 |
| Message-ID | <nq1ct2$mfr$1@adenine.netfront.net> |
| In reply to | #92849 |
On 08/29/2016 01:58 AM, dakupoto@gmail.com wrote: > Could some C language guru please help ? > I am using gcc on Fedora 18. This probably has no direct link to your issue, but Fedora 18 has been EOL, and has received no official updates, to GCC or anything else, since January 14, 2014. https://fedoraproject.org/wiki/End_of_life --- news://freenews.netfront.net/ - complaints: news@netfront.net ---
[toc] | [prev] | [next] | [standalone]
| From | Joe Pfeiffer <pfeiffer@cs.nmsu.edu> |
|---|---|
| Date | 2016-08-29 09:10 -0600 |
| Message-ID | <1bfupn8vbq.fsf@pfeifferfamily.net> |
| In reply to | #92849 |
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);
>
> 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
>
> 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.
I have no better idea than anyone else commenting why the 'R' isn't
being printed (is it possible when cutting this out of your program to
post here, you made a mistake? The only four places of precision in
50.00 is odd, as pointed out by someone else). But, two suggestions:
(1) use a debugger to trace through your program and what b contains
after the sprintf() call. That will at least help narrow down where
the problem lies.
(2) Instead of sprintf(), use snprintf() to protect yourself from buffer
overruns.
--
"Erwin, have you seen the cat?" -- Mrs. Shrödinger
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2016-08-29 10:27 -0700 |
| Message-ID | <ln1t17cwp8.fsf@kst-u.example.com> |
| In reply to | #92849 |
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"
[toc] | [prev] | [next] | [standalone]
| From | Ike Naar <ike@iceland.freeshell.org> |
|---|---|
| Date | 2016-08-29 19:51 +0000 |
| Message-ID | <slrn3vfsns94hc.iik.ike@iceland.freeshell.org> |
| In reply to | #92876 |
On 2016-08-29, Keith Thompson <kst-u@mib.org> wrote: > View the output file with some tool that shows its contents > unambiguously, such as `cat -A` or `od -c`. I've noticed that not all versions of 'cat' endorse the '-A' option, for a 'cat' that doesn't, try 'cat -vet'.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2016-08-29 14:30 -0700 |
| Message-ID | <lntwe3b6va.fsf@kst-u.example.com> |
| In reply to | #92893 |
Ike Naar <ike@iceland.freeshell.org> writes:
> On 2016-08-29, Keith Thompson <kst-u@mib.org> wrote:
>> View the output file with some tool that shows its contents
>> unambiguously, such as `cat -A` or `od -c`.
>
> I've noticed that not all versions of 'cat' endorse the '-A' option,
> for a 'cat' that doesn't, try 'cat -vet'.
Quite right. I think `cat -A` is a GNU coreutils feature, so it will be
available on most Linux-based systems. I think the BSD-based `cat` on
Mac OSX doesn't support it.
`hd` or `hexdump` might also be a good choice (if available).
--
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"
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-08-30 10:56 +0100 |
| Message-ID | <87a8fuy3yw.fsf@bsb.me.uk> |
| In reply to | #92898 |
Keith Thompson <kst-u@mib.org> writes: > Ike Naar <ike@iceland.freeshell.org> writes: >> On 2016-08-29, Keith Thompson <kst-u@mib.org> wrote: >>> View the output file with some tool that shows its contents >>> unambiguously, such as `cat -A` or `od -c`. >> >> I've noticed that not all versions of 'cat' endorse the '-A' option, >> for a 'cat' that doesn't, try 'cat -vet'. > > Quite right. I think `cat -A` is a GNU coreutils feature, so it will be > available on most Linux-based systems. I think the BSD-based `cat` on > Mac OSX doesn't support it. > > `hd` or `hexdump` might also be a good choice (if available). Or "sed -ne l" which is in POSIX (you can write "sed -nel" if that's easier to remember). -- Ben.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web