mIRC Homepage

Appending to end of file

Posted By: Thels

Appending to end of file - 10/09/10 10:03 AM

I'm using /fwrite to write to and append to logfiles. I was under the assumption that /fwrite would append it to the end of the file, but it turns out it writes at the start of the file, overwriting the old data.

Now, it's pretty clear to me that I need to use the /fseek command to set the pointer to the end of the file before using /fwrite, but what is the best way of doing that? /fseek has several switches, but it's not exactly clear what each switch does, especially the -n switch.

Quote:
/fseek <name> <position>

Sets the read/write pointer to the specified position in the file. The following switches may also be used to move the file pointer:

-l <name> <line number>
-n <name>
-w <name> <wildcard>
-r <name> <regex>


What's the recommended command to place the pointer at the end of the file? For what it matters, all lines are written with the -n switch, and thus end with $crlf.
Posted By: Riamus2

Re: Appending to end of file - 10/09/10 11:12 AM

Probably the easiest way is to use $lines and/or track the line number by incrementing it every time you /fwrite to the file.
Posted By: Chessnut

Re: Appending to end of file - 10/09/10 11:52 AM

If you are only accessing the file to append to the end of it, your best bet would probably be to simply use write -a.

Otherwise, you'll need to use something like

Code:
fseek <NAME> $file(<FILE>).size


FYI, fseek -n moves the pointer onto the next line.
Posted By: Thels

Re: Appending to end of file - 10/09/10 11:56 AM

Using lines require fseek to scan for all the newline values, right? Then I think Chessnut's suggestion would work best.

Thanks both of you.

And I'm only accessing it to append, but it's quite possible that I'm adding hundreds of lines, so I prefer to use /fwrite smile
Posted By: genius_at_work

Re: Appending to end of file - 10/09/10 01:42 PM

This sounds like a good feature suggestion. A new switch for /fopen that would position the file pointer at the end of the file for appending.

/fopen -a handle file.txt



-genius_at_work
© 2019 mIRC Discussion Forums