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


Groups > comp.lang.c > #92871

Re: Request help -- odd sprintf, fprintf issue

From Joe Pfeiffer <pfeiffer@cs.nmsu.edu>
Newsgroups comp.lang.c
Subject Re: Request help -- odd sprintf, fprintf issue
Date 2016-08-29 09:10 -0600
Organization A noiseless patient Spider
Message-ID <1bfupn8vbq.fsf@pfeifferfamily.net> (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);
>
> 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

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