Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #92851

Re: Request help -- odd sprintf, fprintf issue

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Request help -- odd sprintf, fprintf issue
Date 2016-08-29 10:30 +0100
Organization A noiseless patient Spider
Message-ID <878tvfly6z.fsf@bsb.me.uk> (permalink)
References <e9765d0c-e510-41ee-9b72-02a85734d45d@googlegroups.com>

Show all headers | View raw


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.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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