Afficher un message
Vieux 11/05/2008, 15h23   #5
Eleanor McHugh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: IO#putc writing 2 bytes?

On 11 May 2008, at 09:58, 7stud -- wrote:
> Jos Backus wrote:
>> On Sun, May 11, 2008 at 03:28:31PM +0900, Minic Minic wrote:
>>> Anyways, I decided to write a SRAM save file editor for a popular
>>> GameBoy game. Problem is for some reason putc is writing 2 bytes
>>> when
>>> ever I try to write 0x0A. It instead writes 0D 0A into the file.

>>
>> Looks like you are on Windows. Try opening @SaveFile in binary mode
>> (@SaveFile
>> = File.open("fname", "wb")) which prevents LF -> CRLF conversion.

>
> Why would that ? The output in the file is 0A 0D, which is a
> windows newline. If the file was not opened in binary mode, that
> means
> the ruby code must have tried to write '\n' to the file, which ruby
> then
> converted to the OS's newline, which for windows is 0A 0D. However,
> getting the string "a + newline" from the user and then chomp()'ing
> off
> the newline should leave you with "a". And using putc() to write the
> string "a" to a file does not involve any newlines.


Because he's not reading or writing 'a' (ASCII character 97) but the
linefeed character (ASCII character 10, otherwise known as CTRL-A).
DOS and her derivatives use the two-character CR LF sequence to
indicate a new line and the C library performs automatic conversion
when a file is opened in text mode. Hence the need to open it in
binary mode instead.

This would be the case in most languages on the Windows platform.


Ellie

Eleanor McHugh
Games With Brains
http://slides.games-with-brains.net
----
raise ArgumentError unless @reality.responds_to? :reason



  Réponse avec citation
 
Page generated in 0,06233 seconds with 9 queries