1. sprintf(): different output on Windows and Linux

sprintf("%03s", {"62"})

on Windows: "062"
on Linux:   " 62" (a blank space instead of zero)

Why? It's supposed to produce the same output on both, isn't it?

new topic     » topic index » view message » categorize

2. Re: sprintf(): different output on Windows and Linux

Julio C. Galaret Viera wrote:
> sprintf("%03s", {"62"})
> 
> on Windows: "062"
> on Linux:   " 62" (a blank space instead of zero)
> 
> Why? It's supposed to produce the same output on both, isn't it?

The "leading zeros" feature of printf/sprintf is really meant
for numeric data, not strings, so I guess the C printf() library
routines that Euphoria depends on, are not compatible in 
this unusual case.

If you do:
   sprintf("%03d", 62)
they both give:
   "062"

Regards,
   Rob Craig
   Rapid Deployment Software
   http://www.RapidEuphoria.com

new topic     » goto parent     » topic index » view message » categorize

3. Re: sprintf(): different output on Windows and Linux

Robert Craig wrote:
> 
> Julio C. Galaret Viera wrote:
> > sprintf("%03s", {"62"})
> > 
> > on Windows: "062"
> > on Linux:   " 62" (a blank space instead of zero)
> > 
> > Why? It's supposed to produce the same output on both, isn't it?
> 
> The "leading zeros" feature of printf/sprintf is really meant
> for numeric data, not strings, so I guess the C printf() library
> routines that Euphoria depends on, are not compatible in 
> this unusual case.
> 
> If you do:
>    sprintf("%03d", 62)
> they both give:
>    "062"
> 
> Regards,
>    Rob Craig
>    Rapid Deployment Software
>    <a href="http://www.RapidEuphoria.com">http://www.RapidEuphoria.com</a>

Yes, I'm aware of being meant for numeric data. I'm using the function to create
name files from strings (not numbers) that when displayed with dir/ls commands
are listed alphabetically (004, 011, 032, and not 11, 32, 4) and when I tried on
Linux they appeared as "\  0", "\  1", "\  2", etc.

It doesn't really matter since I can convert the strings to numeric format
before passing them to sprintf() function, but I want to know what it may be
caused by.

Thanks.

JG

new topic     » goto parent     » topic index » view message » categorize

4. Re: sprintf(): different output on Windows and Linux

Julio C. Galaret Viera wrote:
> 
> Robert Craig wrote:
> > 
> > Julio C. Galaret Viera wrote:
> > > sprintf("%03s", {"62"})
> > > 
> > > on Windows: "062"
> > > on Linux:   " 62" (a blank space instead of zero)
> > > 
> > > Why? It's supposed to produce the same output on both, isn't it?
> > 
> > The "leading zeros" feature of printf/sprintf is really meant
> > for numeric data, not strings, so I guess the C printf() library
> > routines that Euphoria depends on, are not compatible in 
> > this unusual case.
> > 
> > If you do:
> >    sprintf("%03d", 62)
> > they both give:
> >    "062"
> > 
> > Regards,
> >    Rob Craig
> >    Rapid Deployment Software
> >    <a href="http://www.RapidEuphoria.com">http://www.RapidEuphoria.com</a>
> 
> Yes, I'm aware of being meant for numeric data. I'm using the function to
> create
> name files from strings (not numbers) that when displayed with dir/ls commands
> are listed alphabetically (004, 011, 032, and not 11, 32, 4) and when I tried
> on Linux they appeared as "\  0", "\  1", "\  2", etc.
> 
> It doesn't really matter since I can convert the strings to numeric format
> before
> passing them to sprintf() function, but I want to know what it may be caused
> by.
> 
> Thanks.
> 
> JG

Hi there,

That's very interesting and maybe good you brought this up.
For my own purposes, i'm pretty sure i always use
  sprintf("%s", {"62"})
or something like that, with no numbers before the 's'.



Take care,
Al

And, good luck with your Euphoria programming!

My bumper sticker: "I brake for LED's"

 From "Black Knight":
"I can live with losing the good fight,
 but i can not live without fighting it".
"Well on second thought, maybe not."

new topic     » goto parent     » topic index » view message » categorize

5. Re: sprintf(): different output on Windows and Linux

Julio C. Galaret Viera wrote:
> 
> sprintf("%03s", {"62"})
> 
> on Windows: "062"
> on Linux:   " 62" (a blank space instead of zero)
> 
> Why? It's supposed to produce the same output on both, isn't it?

Yes it is. The windows one is wrong (AFAIK)

if you want a printed field of 3, and only 3 characters, you should use %3.3s.
if you want it justified to the left you should use %-3.3s.

The leading zero is generally only used with digits, so with strings the number
03 should be interpreted as 3, whereas with a number it should be interpreted as
'fill up empty spaces with zeros' - I think!

Chris


http://members.aol.com/chriscrylex/euphoria.htm
http://uboard.proboards32.com/
http://members.aol.com/chriscrylex/EUSQLite/eusql.html

new topic     » goto parent     » topic index » view message » categorize

6. Re: sprintf(): different output on Windows and Linux

Chris Burch wrote:
> 
> Julio C. Galaret Viera wrote:
> > 
> > sprintf("%03s", {"62"})
> > 
> > on Windows: "062"
> > on Linux:   " 62" (a blank space instead of zero)
> > 
> > Why? It's supposed to produce the same output on both, isn't it?
> 
> Yes it is. The windows one is wrong (AFAIK)
> 
> if you want a printed field of 3, and only 3 characters, you should use %3.3s.
> if you want it justified to the left you should use %-3.3s.
> 
> The leading zero is generally only used with digits, so with strings the
> number
> 03 should be interpreted as 3, whereas with a number it should be interpreted
> as 'fill up empty spaces with zeros' - I think!
> 
> Chris
> 
> 
> <a
> href="http://members.aol.com/chriscrylex/euphoria.htm">http://members.aol.com/chriscrylex/euphoria.htm</a>
> <a href="http://uboard.proboards32.com/">http://uboard.proboards32.com/</a>
> <a
> href="http://members.aol.com/chriscrylex/EUSQLite/eusql.html">http://members.aol.com/chriscrylex/EUSQLite/eusql.html</a>

As you say, «generally» used with numbers, but in this case I have sequences
with characters from 0 to 9. So I didn't know what the behaviour of the sprint()
function would be if I mixed a format string designed to be used with numbers and
a sequence.
The reason I did that way (that is, not using the correct format string for
sequences you mention) is that being files, I don't want leading blank spaces at
the begining of their names.
After realizing (on Windows) that the output was the same as expected with
numbers, I decided to use it without converting the strings to numbers.

To my surprise when I ported the program to Linux, file names were created the
other way. The OS wisely puts a "\" at the begining of the name, but the "\" sign
in front of a file name puts me a litle nervous.
 
I also think the function behaviour on Linux is what one may expect according to
the normal use of sprintf()... but Windows doesn't think so!

JG

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu