Difference between revisions of "Category:File Handling"

From Scriptwiki
Jump to: navigation, search
m (test preview)
m (better overview)
Line 17: Line 17:
 
#read this line (using $fread)
 
#read this line (using $fread)
 
#close the file (using fclose) again
 
#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)

Revision as of 19:17, 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:

  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.