@FILEOPEN[filename,r[ead]|w[rite]|a[ppend][,b|t]] : Opens the file in the specified mode and returns the file handle as an integer. The optional third parameter controls whether the file is opened in binary or text mode. Text mode (the default) should be used to read text using @FILEREAD without a length, and to write text using @FILEWRITE. Binary mode should be used to read binary data with @FILEREAD with a length, and to write binary data with @FILEWRITEB. Returns -1 if the file cannot be opened.

 

Filename must be in quotes if it contains white space or special characters. To read from standard input, use CON: for the filename.

 

To open a file for both reading and writing, open it in append mode, then use @FILESEEK to position to the start of the file (or any other desired location) before performing additional operations.

 

@FILEOPEN can also open named pipes. The pipe name must begin with \\.\pipe\. @FILEOPEN first tries to open an existing pipe; if that fails it tries to create a new pipe. Pipes are opened in blocking mode, duplex access, byte-read mode, and are inheritable. @FILEOPEN will not return until another process connects to the pipe. For more information on named pipes see your Windows documentation.

 

@FILEOPEN can open file streams on NTFS drives if the stream name is specified. See NTFS File Streams for additional details on file streams.

 

You must reference the file exclusively using the returned file handle, and you must close the file using the file handle. This is especially important when you are debugging a batch program which uses @FILEOPEN. If you suspect that file handles have been opened and not closed, you should restart TCC.

 

Examples:

 

set h=%@fileopen["d:\path\myfile.txt",write]

echo writing %@filewrite[%h,this is a test] bytes

echo closing handle #%h: %@fileclose[%h]

 

See also the related handle-based functions:

 

@FILECLOSEClose a file handle
@FILEREADRead next line from a file handle
@FILESEEKMove a file handle pointer
@FILESEEKLMove a file handle pointer to a specified line
@FILEWRITEWrite next line to a file handle
@FILEWRITEBWrite data to a file handle
@TRUNCATETruncate the file at the current position of the file handle pointer.