Trouble with printing newlines to a file

Discussion in 'Perl Misc' started by shaneal, May 12, 2007.

  1. shaneal

    shaneal Guest

    I want to be able to print just an "LF" to a file.

    First, to clear up any ambiguity, I'm going to call the ASCII
    character with a hex code of 0A and a decimal code of 10 an "LF". I'm
    also going to call the ASCII character with a hex code of 0D and a
    decimal code of 15 a "CR".

    As a simple test case:

    open TEST, ">", "test.data";
    select TEST;
    print "A" . chr(hex("0A")) . "B" . "\n" . "C" . "\r" . "D";
    close TEST;

    Logically, when I look at the test.data file with a hex editor I'd
    expect just a "LF" between the A and B, but instead there are is a
    "CR" followed by an "LF."

    Here is what a hex editor shows is in "test.data" (first line is hex,
    second line are chars):

    41 0D 0A 42 0D 0A 43 0D 44
    A CR LF B CR LF C CR D

    I was expecting only a "LF" (i.e., 0A) between the "A" (41) and
    "B" (42) ...

    If it helps this is Perl 5.8.8 (ActiveState Perl Build 820) on Windows
    XP.

    Thanks for any advice you may be able to give me.
     
    shaneal, May 12, 2007
    #1
    1. Advertising

  2. shaneal

    Dr.Ruud Guest

    shaneal schreef:

    > I want to be able to print just an "LF" to a file.
    > [...] Windows


    perldoc -f binmode

    (search on 'Windows' in there)

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, May 12, 2007
    #2
    1. Advertising

  3. shaneal

    Peter Wyzl Guest

    "shaneal" <> wrote in message
    news:...
    >I want to be able to print just an "LF" to a file.
    >
    > First, to clear up any ambiguity, I'm going to call the ASCII
    > character with a hex code of 0A and a decimal code of 10 an "LF". I'm
    > also going to call the ASCII character with a hex code of 0D and a
    > decimal code of 15 a "CR".
    >
    > As a simple test case:
    >
    > open TEST, ">", "test.data";
    > select TEST;
    > print "A" . chr(hex("0A")) . "B" . "\n" . "C" . "\r" . "D";
    > close TEST;
    >
    > Logically, when I look at the test.data file with a hex editor I'd
    > expect just a "LF" between the A and B, but instead there are is a
    > "CR" followed by an "LF."
    >
    > Here is what a hex editor shows is in "test.data" (first line is hex,
    > second line are chars):
    >
    > 41 0D 0A 42 0D 0A 43 0D 44
    > A CR LF B CR LF C CR D
    >
    > I was expecting only a "LF" (i.e., 0A) between the "A" (41) and
    > "B" (42) ...
    >
    > If it helps this is Perl 5.8.8 (ActiveState Perl Build 820) on Windows
    > XP.


    Given that it's Windows, you may need to binmode the file so the OS doesn't
    make wrong assumptions about your file.

    open TEST, ">", "test.data";
    select TEST;
    binmode TEST;

    perldoc binmode

    P
    Of course I could be on the wrong track entirely....
     
    Peter Wyzl, May 12, 2007
    #3
  4. shaneal

    shaneal Guest

    thanks you both very much.

    i wasn't familiar with binmode. After reading some docs, it worked
    flawlessly
     
    shaneal, May 12, 2007
    #4
  5. Pinocchio <> wrote:
    > On 12 May 2007 04:13:47 -0700, shaneal <> wrote:



    >>i wasn't familiar with binmode.


    > bin mode is


    [snip]

    > a neutral eunich term!



    binmode is not a eunich's term, it is a Perl term.

    It is the same term when used on other "operating systems"
    too (such as the OP's).


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, May 12, 2007
    #5
  6. shaneal

    Dr.Ruud Guest

    A. Sinan Unur schreef:

    > Also, if one wants specific characters, it is better to specify them
    > by their codes rather than using \n whose meaning depends on what the
    > platform thinks a line ending ought to be.



    Using either "\n" of chr(10) or "\x0A" or "\x{0A}" or "\012" or "\cJ"
    (etc.) seems to make no difference (on Windows, have not tested OS-X).

    So just don't forget to call binmode, if the platform's line ending is
    not what you want:


    C:\>perl -we "print chr(10)" > test.out
    C:\>debug test.out
    -d
    14C6:0100 0D 0A 00 00 00 00 00 00-[...]
    -r
    AX=0000 BX=0000 CX=0002 DX=0000[...]



    C:\>perl -we "binmode STDOUT; print chr(10)" > test.out
    C:\>debug test.out
    -d
    14C6:0100 0A 0A 00 00 00 00 00 00-[...]
    -r
    AX=0000 BX=0000 CX=0001 DX=0000[...]


    (the value in CX is the size of the file)


    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, May 13, 2007
    #6
  7. On 2007-05-13 13:36, Dr.Ruud <> wrote:
    > A. Sinan Unur schreef:
    >> Also, if one wants specific characters, it is better to specify them
    >> by their codes rather than using \n whose meaning depends on what the
    >> platform thinks a line ending ought to be.

    >
    >
    > Using either "\n" of chr(10) or "\x0A" or "\x{0A}" or "\012" or "\cJ"
    > (etc.) seems to make no difference (on Windows, have not tested OS-X).


    Yes, "\n" is the same as "\x0A" on Windows. I think it's the same on
    MacOS X, but it was "\x0D" on MacOS <= 9.

    I don't know what EBCDIC-based versions of perl use: NL ("\x15") is
    probably more natural than LF ("\x25") there.

    hp


    --
    _ | Peter J. Holzer | I know I'd be respectful of a pirate
    |_|_) | Sysadmin WSR | with an emu on his shoulder.
    | | | |
    __/ | http://www.hjp.at/ | -- Sam in "Freefall"
     
    Peter J. Holzer, May 13, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Philips

    Reading newlines from a text file

    Thomas Philips, Feb 28, 2004, in forum: Python
    Replies:
    5
    Views:
    414
    Peter Otten
    Feb 29, 2004
  2. Jeffrey E. Forcier

    Printing w/o newlines inside loops - odd behavior

    Jeffrey E. Forcier, Sep 13, 2005, in forum: Python
    Replies:
    3
    Views:
    319
    Jeffrey E. Forcier
    Sep 13, 2005
  3. Dick Davies
    Replies:
    1
    Views:
    121
    Gavin Sinclair
    Sep 29, 2005
  4. Wes Gamble
    Replies:
    4
    Views:
    135
    Wes Gamble
    Aug 3, 2006
  5. Wes Gamble
    Replies:
    7
    Views:
    105
    Wes Gamble
    Dec 14, 2006
Loading...

Share This Page