Jump to content

write to a specific line


Xabache
 Share

Recommended Posts

1. The easiest way is to use the file() function, and then target the line by the index of the generated array. You could also instead call fgets() multiple times until you reach the line you want to read from.2. Similarly to reading, you could use file() to get an array of lines, and then implode it and overwrite the whole file. A more RAM efficient solution (but more HDD demanding one) is to create a new file, use copy all lines up until the one you want to overwrite, write it, and copy the rest. If you want to just insert data on a line (and not replace a line), you can skip the extra file, and just use fgets() until you reach the line, and use fwrite() to write to the line. This is both HDD and RAM efficient (compared to file(), which is not RAM efficient, as it loads the whole file into an array).

Link to comment
Share on other sites

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
 Share

×
×
  • Create New...