REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 6-Feb-2009 Edit History  

REBOL 3 Concepts: Files: Line Conversion

Pending Revision

This document was written for R2 and has yet to be revised for R3.

When a file is read as text, all line terminators are converted to newline (line feed) characters. Line feeds (used as line terminators on Amiga, Linux, and UNIX systems), carriage returns (used as line terminators on Macintosh), and the CR/LF combination (PC and Internet) are all converted to the equivalent newline characters.

Using a standard line terminator within scripts allows them to operate in a machine-independent fashion. For example, to search for and count all newline characters within a text file:

text: read %file.txt
count: 0
while [spot: find text newline][
    count: count + 1
    text: next spot

The line conversion is also useful for reading network files:

text: read

When a file is written, the newline character is converted to the line termination style standard for the operating system being used. For instance, the newline character is converted to a CRLF on the PC, LF on UNIX or Amiga, or CR for a Macintosh. Network files are written with CRLF.

The following function converts any text file with any terminator style to that used by the local operating system:

convert-lines: func [file] [write file read file]

The file is read and all line terminators are converted, then the file is written and newline characters are converted to the local operating system style.

Line conversion can be disabled by reading the file as binary. For instance, the following line:

write/binary %newfile.txt read/binary %oldfile.txt

preserves the line terminators of the text file.

  TOC < Back Next > - WIP Wiki Feedback Admin