deleting line
#1

when i'm freading a file and i want to delete the last line i readed, how do i do it?

thank you
Reply
#2

Just can't just remove the last line from a file.

You could read the file, line by line, and write each line to a second file and skip the last line.
Then delete the first (original) file and rename the second file to the original filename.

Or create an array big enough to read the entire file at once, overwrite the file with the data in the array but skip the last line.
Reply
#3

Quote:
Originally Posted by PowerPC603
Посмотреть сообщение
Just can't just remove the last line from a file.

You could read the file, line by line, and write each line to a second file and skip the last line.
Then delete the first (original) file and rename the second file to the original filename.

Or create an array big enough to read the entire file at once, overwrite the file with the data in the array but skip the last line.
that kind of action seem to slow the server and create lags, am i right? how critical is it?
Reply
#4

How big is your file?
And how many times do you plan to remove the last line from that file?
Once every minute? Once per hour? Or per second?

Even if it's every second, if your file is small you won't have lag.

My script dumps data to a file whenever a change is made in-game.
Like my mission vehicles.
I can park them in another location and the entire file is rewritten from scratch and is 61kB large because it's dumping all data (vehiclemodel, coordinates and rotation, color) about every mission-vehicle on my server into that file.
Writing such a file only takes a fraction of a second, you don't even notice a minor lag-spike.
Even when I'm spamming my /park command, I don't notice any lag.



If you plan to do this with a file which is several megabytes in size, and you wanna do that several times per second, then yes, you'll get lag.
But then you would need to consider moving to a database instead of a file.
Then you can do what you want without rewriting the entire database or file.
Using MySQL for example, you can have one column in a table which holds text (and another to hold the id, like a linenumber).
Then you can easily delete ANY line from that database with a single query, which is even executed in a threaded way, so you won't get any lag as the query will be executed in a separate thread and won't hog your server until it's completed (kinda like a send-and-forget order).
Reply
#5

Quote:
Originally Posted by PowerPC603
Посмотреть сообщение
How big is your file?
And how many times do you plan to remove the last line from that file?
Once every minute? Once per hour? Or per second?

Even if it's every second, if your file is small you won't have lag.

My script dumps data to a file whenever a change is made in-game.
Like my mission vehicles.
I can park them in another location and the entire file is rewritten from scratch and is 61kB large because it's dumping all data (vehiclemodel, coordinates and rotation, color) about every mission-vehicle on my server into that file.
Writing such a file only takes a fraction of a second, you don't even notice a minor lag-spike.
Even when I'm spamming my /park command, I don't notice any lag.



If you plan to do this with a file which is several megabytes in size, and you wanna do that several times per second, then yes, you'll get lag.
But then you would need to consider moving to a database instead of a file.
Then you can do what you want without rewriting the entire database or file.
Using MySQL for example, you can have one column in a table which holds text (and another to hold the id, like a linenumber).
Then you can easily delete ANY line from that database with a single query, which is even executed in a threaded way, so you won't get any lag as the query will be executed in a separate thread and won't hog your server until it's completed (kinda like a send-and-forget order).
Thank you, you helped me a lot.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)