Difference between revisions of "Category:File Handling"

From Scriptwiki
Jump to: navigation, search
(slightly better hopefully?)
 
(8 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
== General Explanation ==
 
== General Explanation ==
Using the commands and identifiers in these 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|/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|/fseek]]). After you've modified you file, you '''have to''' close it again (to unlock it and so make it accessable by other applications) using [[Fclose|/fclose]].
+
In addition to several high-level file access methods, mIRC offers a low-level way of accessing files, which closely resembles the way that operating systems like Windows or UNIX allow programs to access files. The individual commands and identifiers in this category allow you to open a file, perform a number of (read/write) operations on it, and close it.
 +
 
 +
First of all you have to open a file using [[Fopen|/fopen]]. When doing this, you have to specify a handle - in subsequent commands you can use this handle to refer to the open file. The handle is just a name that you, the scripter, can choose yourself. This is similar to how [[:Category:Socket|socket]] names work.
 +
 
 +
Each open file has a ''file pointer''. This file pointer is the current byte position in the file, and the read/write commands will always work from this position. Additionally, those commands will move the file pointer forward with as many bytes as they read/write. For example, if the file pointer is at byte 1055, and you use [[$fgetc]], then you will get the 1055th byte of the file, and the file pointer will move to 1056. When you've just opened the file, the file pointer is set to the start of the file (byte 0); you can manually move the pointer to another position in the file by using [[Fseek|/fseek]].
 +
 
 +
mIRC will lock an opened file from being accessed by other programs for as long as the file handle is open. After you've used a file, you '''have to''' close it again, using [[Fclose|/fclose]]. That will write any changes you made to disk, and unlock the file.
 +
 
 +
'''Note''' that you should check [[$ferr]] after every operation on the file (including opening it!).
  
 
== Difference to /write and $read ==
 
== Difference to /write and $read ==
Line 6: Line 14:
 
  [[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)
+
#open the file (using fopen)
*move the pointer to the start of line 4 (using fseek)  
+
#read the first line (using $fread - check [[$read]] for more details about it)
*read this line using $fread
+
#move the pointer to the start of line 4 (using fseek)  
*close the file (using fclose)
+
#read this line (using $fread)
*open the file again (using fopen)
+
#close the file (using fclose)
*move the pointer to the start of line 5 (using fseek)  
+
#open the file again (using fopen)
*read this line using $fread
+
#move the pointer to the start of line 5 (using fseek)  
*close the file (using fclose) again
+
#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)
+
#open the file (using fopen)
*jump to line 4 (using fseek)
+
#jump to line 4 (using fseek)
*read this line (using fread)
+
#read this line (using fread)
*read the next line (and the pointer is automatically there after reading line 4 - using fread)
+
#read the next line (and the pointer is automatically there after reading line 4 - using fread)
*close the file (using flose)
+
#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 ==

Latest revision as of 00:58, 23 December 2005

General Explanation

In addition to several high-level file access methods, mIRC offers a low-level way of accessing files, which closely resembles the way that operating systems like Windows or UNIX allow programs to access files. The individual commands and identifiers in this category allow you to open a file, perform a number of (read/write) operations on it, and close it.

First of all you have to open a file using /fopen. When doing this, you have to specify a handle - in subsequent commands you can use this handle to refer to the open file. The handle is just a name that you, the scripter, can choose yourself. This is similar to how socket names work.

Each open file has a file pointer. This file pointer is the current byte position in the file, and the read/write commands will always work from this position. Additionally, those commands will move the file pointer forward with as many bytes as they read/write. For example, if the file pointer is at byte 1055, and you use $fgetc, then you will get the 1055th byte of the file, and the file pointer will move to 1056. When you've just opened the file, the file pointer is set to the start of the file (byte 0); you can manually move the pointer to another position in the file by using /fseek.

mIRC will lock an opened file from being accessed by other programs for as long as the file handle is open. After you've used a file, you have to close it again, using /fclose. That will write any changes you made to disk, and unlock the file.

Note that you should check $ferr after every operation on the file (including 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:

  1. open the file (using fopen)
  2. read the first line (using $fread - check $read for more details about it)
  3. move the pointer to the start of line 4 (using fseek)
  4. read this line (using $fread)
  5. close the file (using fclose)
  6. open the file again (using fopen)
  7. move the pointer to the start of line 5 (using fseek)
  8. read this line (using $fread)
  9. 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:

  1. open the file (using fopen)
  2. jump to line 4 (using fseek)
  3. read this line (using fread)
  4. read the next line (and the pointer is automatically there after reading line 4 - using fread)
  5. 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).

Example

Pages in category "File Handling"

The following 13 pages are in this category, out of 13 total.