Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device.
Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
This is one of the ‘gotchas’ that can impact 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 the Unix based world uses\n.
Here’s a code fragment which shows the problem on windows:
Take a careful look at how in the non binary mode the new line sequences are translated - \r\n to \n.
Notice how when we read in text mode, the \r\n sequences are translated to \n. But in binary, the data isn’t altered. This can really mess up your files!
Don’t forget to import the . 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