mIRC Home    About    Download    Register    News    Help

Print Thread
Appending to end of file #225810 10/09/10 10:03 AM
Joined: Aug 2010
Posts: 134
T
Thels Offline OP
Vogon poet
OP Offline
Vogon poet
T
Joined: Aug 2010
Posts: 134
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.


Learning something new every day.
Re: Appending to end of file [Re: Thels] #225814 10/09/10 11:12 AM
Joined: Oct 2004
Posts: 8,327
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,327
Probably the easiest way is to use $lines and/or track the line number by incrementing it every time you /fwrite to the file.


Invision Support
#Invision on irc.irchighway.net
Re: Appending to end of file [Re: Thels] #225815 10/09/10 11:52 AM
Joined: May 2007
Posts: 37
C
Chessnut Offline
Ameglian cow
Offline
Ameglian cow
C
Joined: May 2007
Posts: 37
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.

Re: Appending to end of file [Re: Chessnut] #225816 10/09/10 11:56 AM
Joined: Aug 2010
Posts: 134
T
Thels Offline OP
Vogon poet
OP Offline
Vogon poet
T
Joined: Aug 2010
Posts: 134
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


Learning something new every day.
Re: Appending to end of file [Re: Thels] #225819 10/09/10 01:42 PM
Joined: Oct 2005
Posts: 1,741
G
genius_at_work Offline
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
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