Jump to content

Getting the next unwritten line of a file?


kidul

Recommended Posts

ye ,i already know how to get the end of a file ... but i don´t know how to get the next line. thanks anyway

You can read some amount of bytes and check if the returned string contains "\r\n" or "\n" or "\r" (depends on which OS the file was saved) with string.find()

What are you trying to do anyway? File functions aren't comfortable to use. There might be better way if you just say what you want to achieve.

Link to comment

i dont understand, you mean writing or reading? if you want to write to the next line just insert a new line character "\n"

dunno if this could work from the lua manual, havent access files with mta

The read function reads strings from the current input file. Its arguments control what is read:

"*all" reads the whole file

"*line" reads the next line

"*number" reads a number

num reads a string with up to num characters

Link to comment
i dont understand, you mean writing or reading? if you want to write to the next line just insert a new line character "\n"

dunno if this could work from the lua manual, havent access files with mta

The read function reads strings from the current input file. Its arguments control what is read:

"*all" reads the whole file

"*line" reads the next line

"*number" reads a number

num reads a string with up to num characters

Leave the Lua "file" library alone. You can't even call its functions due to MTA protection... That's why you have file functions.

Link to comment

OK, 50p is correct, (and therefore I learned something :D ) so here's some code that should work out:

myFile = fileOpen ("pathto/myfile.txt")  --forwardslash prevents issues with escaping characters - thanks 50p
local sizeOfFile = fileGetSize ( myFile )
fileSetPos ( myFile, ( sizeOfFile - 1 ) )  --apparently 1 byte
local testString = fileRead ( myFile, 1 )  -- and again
if testString ~= "\n" then    --we want the compiler to parse this as a magic character here
fileWrite ( myFile, "\n" )    --change to "\r\n" if you care about correct display on certain Windows programs.  Windows uses \r\n, Linux uses \n, and IIRC, only Macs use just \r.
end

This will open your file, move to 2 bytes before the end of the file, check if it is a new line, and if not, write a new line.

fileRead moves the position through the file as it reads, as does fileWrite, so now the current position would be the end of the file, ready to append data.

Personally I wouldn't do a fileFlush at this point. If your script doesn't ever actually write anything to the file from here, there is little point in just saving an additional newline.

Edited by Guest
Link to comment

subenji99,

As far as I know "\n" is just 1 byte therefore you'd move back to "\r\n" (if it was windows format) or "X\n" (where X is the last character at the last line).

Also, I do not recommend using backslash (\) in file paths... Because that's escape character (of course you can use \\ to escape backslash...) and would only work on windows which is not a good idea. Use forward slash (/) instead. It solves the previously mentioned problem.

Besides wait for reply from the author of the topic to find out what he's trying to do. Maybe he's attempting to do something that would be more comfortable with XML or SQLite.

Link to comment

If you have the know-how to fix that up, it would be a good idea to do so, and post it.

Not just to correct me, or even to answer the original poster's original question - but to teach anyone that happens to find this topic about the file functions and their proper usage.

"That is wrong" is only helpful to me. Commented, readable code is helpful to anyone that reads it. A under-documented set of functions have been discussed here, and it's worth informing people on proper usage.

Link to comment

Leave the Lua "file" library alone. You can't even call its functions due to MTA protection... That's why you have file functions.

I know some libraries are blocked but not all (math.*). MTA functions returns a file handle, i didnt know if that handle could be a lua file handle, in which case this maybe could be done:

local theLine
local fhandle = fileOpen("test.txt")             -- attempt to open the file
if fhandle then                                              -- check if it was successfully opened
   theLine = fhandle:read("*line")               --read a line
end
fileClose(hFile)

As i said i havent used files myself inside MTA was just dropping the idea.

Link to comment

Leave the Lua "file" library alone. You can't even call its functions due to MTA protection... That's why you have file functions.

I know some libraries are blocked but not all (math.*). MTA functions returns a file handle, i didnt know if that handle could be a lua file handle, in which case this maybe could be done:

local theLine
local fhandle = fileOpen("test.txt")             -- attempt to open the file
if fhandle then                                              -- check if it was successfully opened
   theLine = fhandle:read("*line")               --read a line
end
fileClose(hFile)

As i said i havent used files myself inside MTA was just dropping the idea.

They don't.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...