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:

[lua]local yearString = fh:read( “*l”)
local monthString = fh:read( “*l”)
local dayString = fh:read( “*l”)[/lua]

Under the simulator, the strings equal “2011”, “02” and “11” as expected. Under Android, they each have a carriage return (13) added to the end.

Is this known behaviour? I’m on Windows. Thanks. [import]uid: 23949 topic_id: 6310 reply_id: 306310[/import]

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. [import]uid: 8872 topic_id: 6310 reply_id: 22896[/import]

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. [import]uid: 23949 topic_id: 6310 reply_id: 22911[/import]

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 [import]uid: 8196 topic_id: 6310 reply_id: 23083[/import]

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 :slight_smile: [import]uid: 23949 topic_id: 6310 reply_id: 23086[/import]