reading lines from text file, end of line handling

(I copied this from another topic of mine)

I was reading lines of text from a file, for example:

2011
02
11

using code like this:

1
2
3
local yearString = fh:read( "*l")
local monthString = fh:read( "*l")
local dayString = fh:read( "*l")

Are you sure you didn't save the file with CR/LF instead of just LF?

Its a common thing on windows where notepad adds CR/LF for each new line. You can use a string find/replace to handle it in your code.

I used notepad, but then why the different behavior between simulator and device? I figured it was a line ending thing so I was able to work around it. I ended checking the byte values of the strings to check for carriage returns and chopping off a character if found.

The reason the behavior is different is that the simulator uses Windows i/o routines to read the text contents, which handles the CR/LFs. But when you build for Android and run on the device it's running under Linux and the i/o routines are expecting just LRs at the end of lines.

Tim

Of course, thanks. Pretty embarrassing considering I'm a Linux & Windows developer. I just don't think like a developer when I'm at home :)

views:1173 update:2011/10/9 9:57:41
corona forums © 2003-2011