Reading a file line by line in Lua

According to the documentation of Lua file:read("*l") reads the next line, skipping the end of the line.

Note: - "* l": reads the next line, skipping the end of the line, returning zero to the end of the file. This is the default format.

Is this the correct documentation? Because file:read("*l") reading the current line, not the next line, or is my understanding wrong? Pretty confusing ...

+6
source share
1 answer

Lua manages files using the same model of the underlying C implementation (this model is also used by other programming languages, and it is quite common). If you are not familiar with this way of viewing files, the terminology may be unclear, indeed.

In this model, the file is represented as a stream of bytes with the so-called current position. The current position is a kind of conceptual pointer to the first byte in the file that will be read or written by the next I / O operation. When you open a file for reading, the new stream is configured so that its current position is the beginning of the file, that is, the current position "points" to the first byte in the file.

In Lua, you control streams through so-called file descriptors, which are a kind of intermediaries for underlying streams. Any operation performed using the handle is transferred to the corresponding thread.

Lua io.open opens the file, associates the C stream with it, and returns the file descriptor that this stream represents:

 local file_handle = io.open( "myfile.txt" ) -- file opened for reading 

Therefore, if you perform some operation that reads some bytes (usually interpreted as characters, if you work with text files), they are read from the stream and for each byte they read the current position of the stream forward, indicating every time until the next byte to read .

Lua documentation implies this model. Thus, when he says the next line, this means that the input operation will read all the characters in the stream, starting from the current position, until the end of line character is found.

Please note that if you look at text files as a sequence of lines, you can be misleading, as you might think about the "current line" and the "next line". This will be a higher-level model than model C. In C. there is no text string C. In C, text files are nothing more than a sequence of bytes where some special characters (end-of-line characters) are subjected to special treatment (which is mainly implementation dependent) and some C standard functions are used as string delimiters, i.e. as labels to detect when characters stop reading.

Another source of confusion for beginners or people coming from higher-level languages ​​is that in C, for a historical accident, bytes are treated as characters (the basic data type for processing single bytes is char , which is the smallest number you type in C!). Therefore, for people with background C, it is natural to think of bytes as characters and vice versa.

Although Lua is a higher-level language than C, its close relationship with C (it was designed to be easily interfaced with C code) makes it an inheritance of C bytes as characters. In fact, for example, Lua strings can contain arbitrary bytes and can be used to process raw binary data.

+4
source

Source: https://habr.com/ru/post/957180/


All Articles