You are viewing an old version of this page. View the current version.
Compare with Current
View Page History
Version 1
Next »
This is one of ‘gotchas’ that can impact on people when they read files. The underlying commands for loading files in operating systems tend to offer two options:
Read in text mode
Read in binary mode
What text mode does is it will translate newline characters to the local operating system convention for newline characters. See IBM, DOS and Windows Newlines to understand why windows expresses newlines with \r\n while the rest of then Unix based world using \n.
Here’s a code fragment which shows the problem on windows:
This code fragment shows the problem:
Take a careful look at how in the non binary mode the new line sequences are translated - \r\n to \n.
This is the same code fragment with big ugly red boxes around the parts of the annotation to pay attention to
If you want to reproduce it here is the code
Don’t forget to import the FIL Library.
require "FIL.FILreadWrite"
function main(Data)
FILwrite("~test.txt", "Freddy\r\nSmith\r\n")
FILread("~test.txt")
local F = io.open("~test.txt",'r')
Result = F:read("*a")
F:close()
return Result
end