Difference between revisions of "Category:File Handling"
m (sp. accessible) |
m (test preview) |
||
Line 8: | Line 8: | ||
[[echo]] -a $read(my.txt,4) $read(my.txt,5) | [[echo]] -a $read(my.txt,4) $read(my.txt,5) | ||
Internally, mIRC would do following: | Internally, mIRC would do following: | ||
− | + | #open the file (using fopen) | |
− | + | #read the first line (using $fread - check [[$read]] for more details about it) | |
− | + | #move the pointer to the start of line 4 (using fseek) | |
− | + | #read this line (using $fread) | |
− | + | #close the file (using fclose) | |
− | + | #open the file again (using fopen) | |
− | + | #move the pointer to the start of line 5 (using fseek) | |
− | + | #read this line (using $fread) | |
− | + | #close the file (using fclose) again | |
That's obviously not efficient, as we don't need to close it after our first read. So using File Handling, we could directly do: | That's obviously not efficient, as we don't need to close it after our first read. So using File Handling, we could directly do: | ||
− | + | #open the file (using fopen) | |
− | + | #jump to line 4 (using fseek) | |
− | + | #read this line (using fread) | |
− | + | #read the next line (and the pointer is automatically there after reading line 4 - using fread) | |
− | + | #close the file (using flose) | |
If you want to read one or two lines only, there is no need to use this way, as $read is alot easier to handle, but if you have a file of e.g. 40mb and want to read 100 lines, there is a ''big'' difference between File Handling and $read (~ factor 10). | If you want to read one or two lines only, there is no need to use this way, as $read is alot easier to handle, but if you have a file of e.g. 40mb and want to read 100 lines, there is a ''big'' difference between File Handling and $read (~ factor 10). | ||
== Example == | == Example == |
Revision as of 19:16, 22 December 2005
General Explanation
Using the commands and identifiers in this category allows you to modify a file directly. That means that mIRC opens a connection to the specified and locks this connection when you are opening a file using /fopen. At the same moment, a (file-)pointer is set to the start of the file. You can now read the character the pointer points to (either by using $fread that reads the entire line until the next $crlf or by using $fgetc that reads the "next" character only - note that the pointer is automatically moved to the begin of the next line / next character by using these identifiers) or move the pointer to another position in the file (by using /fseek). After you've modified you file, you have to close it again (to unlock it and so make it accessible by other applications) using /fclose.
Note that you should check $ferr after every modification of the file (inclusive opening it).
Difference to /write and $read
Contrary to this, the "usual" way to access a file (/write and $read that is) work in another way. For example, if you want to echo line number 4 and 5 of a file, you would use:
echo -a $read(my.txt,4) $read(my.txt,5)
Internally, mIRC would do following:
- open the file (using fopen)
- read the first line (using $fread - check $read for more details about it)
- move the pointer to the start of line 4 (using fseek)
- read this line (using $fread)
- close the file (using fclose)
- open the file again (using fopen)
- move the pointer to the start of line 5 (using fseek)
- read this line (using $fread)
- close the file (using fclose) again
That's obviously not efficient, as we don't need to close it after our first read. So using File Handling, we could directly do:
- open the file (using fopen)
- jump to line 4 (using fseek)
- read this line (using fread)
- read the next line (and the pointer is automatically there after reading line 4 - using fread)
- close the file (using flose)
If you want to read one or two lines only, there is no need to use this way, as $read is alot easier to handle, but if you have a file of e.g. 40mb and want to read 100 lines, there is a big difference between File Handling and $read (~ factor 10).