Re: [Rd] a little bug for the function 'sprintf' (PR#14161)

From: Ted Harding <Ted.Harding_at_manchester.ac.uk>
Date: Mon, 21 Dec 2009 18:15:01 +0000 (GMT)


On 21-Dec-09 17:36:12, Peter Dalgaard wrote:
> baoliqin@gmail.com wrote:

>> Dear R-ers,
>> I am a gratuate student from South China University of Technology.
>> I fond the function 'sprintf' in R2.10.1 have a little bug(?):
>> 
>> When you type in the example codes:
>> 
>>> sprintf("%s is %f feet tall\n", "Sven", 7.1)
>> 
>> and R returns:
>> 
>> [1] "Sven is 7.100000 feet tall\n"
>> 
>> this is very different from the 'sprintf' function in C/C++,
>> for in C/C++, the format string "\n" usually represents a new line,
>> but here, just the plain text "\n"!

>
> No, this is exactly the same as in C/C++. If you compare the result
> of sprintf to "Sven is 7.100000 feet tall\n" with strcmp() in C,
> they will compare equal.
>
> > s <- sprintf("%s is %f feet tall\n", "Sven", 7.1)
> > s
> [1] "Sven is 7.100000 feet tall\n"
> > nchar(s)
> [1] 27
> > substr(s,27,27)
> [1] "\n"
>
> The thing that is confusing you is that strings are DISPLAYED
> using the same escape-character mechanisms as used for input.
> Compare
>
> > cat(s)
> Sven is 7.100000 feet tall
> >
>
>> 
>> Is it a bug, or a deliberate design?

>
> Design, not bug (and please don't file as bug when you are in doubt.)

And another confusion is that the C/C++ function sprintf() indeed creates a string AND ASSIGNS IT TO A NAMED VARIABLE, according to the syntax

  int sprintf(char *str, const char *format, ...);

as in

  char *X ;
  sprintf(X,"%s is %f feet tall\n", "Sven", 7.1) ;

as a result of which the string X will have the value   "Sven is 7.100000 feet tall\n"

R's sprintf does not provide for the parameter "Char *str", here X, and so RETURNS the string as the value of the function.

This is NOT TO BE CONFUSED with the behaviour of the C/C++ functions printf() and fprintf(), both of which create the string and then send it to either stdout or to a file:

  int printf(const char *format, ...);
  int fprintf(FILE *stream, const char *format, ...);

Therefore, if you programmed

  printf("%s is %f feet tall\n", "Sven", 7.1) ;

you would see on-screen (stdout) the string

  "Sven is 7.100000 feet tall"

(followed by a line-break due to the "\n"), while

  mystream = fopen("myoutput.txt",a) ;
  fprintf(mystream, "%s is %f feet tall\n", "Sven", 7.1) ;

would append

  "Sven is 7.100000 feet tall"

(followed by a line-break) to myoutput.txt

Hoping this helps!
Ted.



E-Mail: (Ted Harding) <Ted.Harding_at_manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861
Date: 21-Dec-09                                       Time: 18:14:57
------------------------------ XFMail ------------------------------

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Mon 21 Dec 2009 - 18:17:54 GMT

This archive was generated by hypermail 2.2.0 : Tue 22 Dec 2009 - 11:51:10 GMT