The function of file operation is also provided in the BCB, and the functions of these functions are substantially the same, but such functions and BCB relationships are close to the BCB's ANSISTRING and other data types, in this way The file operation is the most convenient, and I will introduce this file in detail below.
In this file operation function provided by BCB, it can be divided into three types, which is: 1, file name function, 2, file management function; 3, file I / O function.
1, file name function
File name functions can operate on the name of the file, the subdirectories, drives, and extensions. The following table lists these functions and its features.
Function description
ExpandFileName () Returns the full path of the file (including drive, path)
Extractfileext () draws extensions from the file name
ExtractFileName () Extract the file name without the path from the file name
ExtractFilePath () extracts the path name from the file name
ExtractFileDir () extracts the directory name from the file name
ExtractFileDrive () extracts the drive name from the file name
Changefileext () changes the extension of the file
ExpanduncFileName () Returns the full path to the file containing the network drive
ExtractRELATIVEPATH () extracts relative path information from the file name
ExtractShortPathname () Transforms file names to DOS 8 · 3 format
Matchesmask () Check if the file matches the specified file name format
Let's introduce these functions below:
(1) ExpadFileName ()
Prototype: extern package anstructing __fastcall expandFileName (const onSition filename);
Function: Return to the full path of the file (including drive, path)
Parameters: filename: file name to handle
Example: ShowMessage (Application-> Exename); // Show your program file name, such as c: /mybcb/sample1.exe
(2 )ExtFileExt ()
Prototype: extern package anstruing __fastcall extractfileext (const onSition filename);
Function: Patting the extension from the file name
Parameters: FileName: The file name (full path) to process
Example: ShowMessage (extractFileExt (Application-> Exename)); // Display ".exe"
ExTractFileName ()
Prototype: Extern package anstruing __fastcall extractFileName (const onSition filename);
Function: Patting the file name without the path from the file name
Parameters: filename: file name to handle
Example: ShowMessage (ExtractFileExt ("c: //winnt//sol.exe")); // Display "Sol.exe"
⑷EXTRACTFILEPATH ()
Prototype: Extern package anstring __fastcall extractFilePath (const onSition filename);
Function: Patting the path name from the file name
Parameters: filename: file name to handle
Example: ShowMessage ("Winnt // Sol.exe")); // Displays "Winnt /"
⑸EXTRACTFILEDIR ()
Prototype: extern package ansistring __fastcall extractFileDir (const ANSISTRING EXTRACTFILEDIR (Const Ansistring FileName); Function: Different from the file name (different from the previous function, excluding the last "/")
Parameters: filename: file name to handle
Example: ShowMessage ("WinntFileDir (" Winnt // Sol.exe ")); // Displays the difference between" Winnt ", pay attention to the previous function
⑹EXTRACTFILED ()
Prototype: Extern package Ansistring __fastcall extractfileDrive (const anstring filename);
Function: Extract the drive name from the file name
Parameters: filename: file name to handle
Example: ShowMessage (ExtractFileDrive ("c: //winnt//sol.exe"))); // Display "C:"
⑺Changefileext ()
Prototype: EXTERN PACKAGE SYSTEM :: ANSISTRING __FASTCALL CHANGEFILEXT (const system :: Ansistring filename, const system :: ansistring extension);
Function: Change the extension of the file name, not to rename the real file, just to process the file name this string
Parameters: FileName: The name of the file to be renamed, Extension: New extension
Example: ShowMessage (CHANGEFILEXT ("C: //winnt//sol.exe", ".ooo")); // Displays "C: /Winnt/SOL.OOO"
⑻EXPANDUNCFILENAME ()
Prototype: extern package anstrunce __fastcall expanduncfilename (const onSISTRING FILENAME);
Function: Returns the full path of the file containing the network drive, the format is: // Machine name / shared name / file name
Parameters: filename: file name to handle
Example: ShowMessage ("f: //winnt//sol.exe")); / * If f: is a mapped network drive // NT40 / Winnt, display "//nt40/winnt/sol.exe" * /
⑼EXTRACTRELATIVEPATH ()
Prototype: Extern package Ansistring __fastcall extractrelativepath (const anstring basename);
Function: Patting relative path information from the file name, such as "../ss/ss.asd"
Parameters: BaseName: Benchmark file name; destname: target file name
Example: ShowMessage ("D: ///source////1.123", "D: //Source//ASM//Dz.asm")); / * Display "../asm/dz.asm "* /
⑽EXTRACTSHORTPATHNAME ()
Prototype: Extern package anstring __fastcall extractShortPathName (const onString filename); Function: Convert file name to DOS 8, 3 format
Parameters: filename: file name to handle
Example: ShowMessage ("E: // Program files // Dual Wheel mouse // 4dmain.exe")); / * Display "E: /PROGRA ~ 1/Dualwh ~ 1/4dmain.exe" * /
⑾matchesmask ()
Prototype: Extern package bool __fastcall matchesmask (const onging filename);
Function: Check if the file matches the specified file name format
Parameters: filename: The name of the file to be processed; Mask: File name format, support wildcard
Example: ShowMessage (Matchesmask ("lxf.exe", "*.? X?)); // Show" True "
-------------------------------------------------- ------------------------------
2, file management function
Such functions include setting and reading various operations related to the drive, subdirectories, and files, which lists the common functions of such operations and their functions.
Function function
CreateDir () Creates a new subdirectory
Deletefile () Delete file
DirectoryExists () judgment if the directory exists
DiskFree () gets the residual space of disk
Disksize () Get disk capacity
FILEEXISTS () determines if the file exists
Filegetttr () Get file properties
FilegetDate () Get the date of file
GetCurrentDir () Get the current directory
RemoveDir () Delete Directory
SetCurrentDir () Sets the current directory
Let's introduce these functions below:
(1 )createdir ()
Prototype: Extern package bool __fastcall createdir (const system :: Ansistring Dir);
Function: Create a subdirectory, if you successfully return true, otherwise returning false
Parameters: Dir: The name of the subdirectory to be established
Example: Create ("ASM"); // Create a subdirectory called ASM in the current directory
(2) DELETEFILE ()
Prototype: extern package bool __fastcall deletefile (const system :: aNSISSTRING FILENAME);
Function: Delete files, return false if it successfully returns true, otherwise return false
Parameters: FileName: The file name to delete
Example: IF (OpenDialog1-> Execute ()) deletefile (OpenDialog1-> filename);
CA2Directoryexists ()
Prototype: Extern package bool __fastcall directoryexists (const system :: ansistring name);
Function: Check if the directory exists, if there is a returns, return false
Parameters: Name: Directory to be detected
Example: if (! Directoryexists ("ASM")) CreateDir ("asm"); // If the ASM is not existed, it creates the ⑷DiskFree ()
Prototype: Extern package __int64 __fastcall diskfree (byte driving); BYTE DRIVE
Function: Detect disk remaining space, return value in byte, if the specified disk is invalid, return -1
Parameters: drive: The code of the disk, 0 means the current disk, 1 = A, 2 = B, 3 = C Based on this class
Example: ShowMessage (DiskFree (0)); // Displays the remaining space of the current disk
⑸DISKSIZE ()
Prototype: extern package __int64 __fastcall disksize; Byte Drive
Function: Detect disk capacity, return value in byte, if the specified disk is invalid, return -1
Parameters: drive: The code of the disk, 0 means the current disk, 1 = A, 2 = B, 3 = C Based on this class
Example: ShowMessage (DiskFree (0)); // Displays the capacity of the current disk
⑹fileexists ()
Prototype: Extern package bool __fastcall fileexists (const onging filename);
Function: Detecting the file exists, if there is a returning true, return false
Parameters: filename: The file name to be detected
Example: IF (FileExists ("aaa.asm")) deletefile ("aaa.asm");
⑺filegetttr ()
Prototype: extern package int __fastcall filegettttr (const anstruing filename);
Function: Get file properties, if an error returns -1
The return value is as follows, if returned $ 00000006 indicates a file with an implicit and system properties (4 2)
Constant value meaning
Fareadonly $ 00000001 Read-only file
FAHIDDEN $ 00000002 implied file
Fasysfile $ 00000004 System file
Favolumeid $ 00000008 Volume
Fadirectory $ 00000010 Directory
Faarchive $ 00000020 Archive file
Example: IF (Filegetttr ("LLL.TXT") & 0x2) ShowMessage ("This is a file with hidden properties");
There is a FileSetattr () corresponding to this, please refer to the help system
⑻filegetdate ()
Prototype: Extern package int __fastcall filegetdate (int Handle);
Function: Returns the number of seconds of the file to 0:00 pm at 0:00 to 1-1
Parameters: Handle: The file handle opened with fileOpen ().
example:
INT i = fileopen ("c: //autoexec.bat", fmopenread);
ShowMessage (FilegetDate (i));
FileClose (i);
There is filesetdate () corresponding to this, please contact the help system
⑼getcurrentdir ()
Prototype: extern package ansiString __fastcall getcurrentdir ();
Function: Get the current directory name: showMessage (getcurrentdir ());
⑽removedir ()
Prototype: Extern package bool __fastcall removedir (const anstring dir);
Function: Delete the directory, return false if you successfully returns true, otherwise return false
Parameters: Dir: Directory to delete
Example: IF (DIECTORYEXISTS ("ASM")) Removedir ("ASM");
⑾SETCURRENTDIR ()
Prototype: Extern package bool __fastcall setcurrentdir (const anestring dir);
Function: Set the current directory, return false if you returned to true, otherwise returns false
Parameters: DIR: Directory name to switch to
Example: setcurrentdir ("c: // windows");
-------------------------------------------------- ------------------------------
3, file I / O function
Such functions complete the read and write related to the file, this type of operation and C based on I / O file operations, the following table lists the common functions of such operations and its functions.
FileOpen () Opens the file
FILECLOSE () Close file
FileRead () read file
Fileseek () file positioning
FileWrite () write files
FileCreate () creates a file
These functions are described in detail below.
(1) FileOpen ()
Prototype: Extern package int __fastcall fileopen (const anstring filename, int mode);
Function: Open the file, if you have returned its handle, or return -1
Parameters: FileName: The file name to open; mode: Opened mode, with the value as follows, "or" ("|") operator connection.
Constant value description
-------------------------------------------------- -----------
FmopenRead 0 opens read-only attributes
FMOpenWrite 1 Open only attribute
FmopenReadWrite 2 opens with read / write properties
FMShareCompat 0 is compatible with FCB mode (there is corresponding DOS function call in assembly, "Interesting self-contained information)
FMShareExClusive 16 sharing method: Open in exclusive way, before closing, others can't access
FMSharednywrite 32 Sharing Method: Refusing to write access
FMSharedenyRead 48 sharing method: refusing to read access
FMSharednynone 64 sharing method: unlimited, allow reading and writing
Example: int i = fileopen ("c: //windows//win.ini", fmopenreadwrite | fmshareExClusive;
(2) FileClose ()
Prototype: Extern package void __fastcall fileclose (int Handle);
Function: Close the open handle.
Parameters: Handle: Handle to close
Example: FileClose (i);
CD, FileRead ()
Prototype: Extern package int __fastcall fileRead (int Handle, Void * Buffer, INT Count); Function: Read file, return the number of bytes actually read, the handle must first be created by FileOpen or FileCreate.
Parameters: Handle: The handle to read; buffer: Saving the buffer of the read data; Count: Want to read the number of bytes
Example: Char Str [400]; FileRead (HND1, STR, 400);
⑷fileseek ()
Prototype: Extern package int __fastcall fileseek (int Handle, int offset, int origin);
Function: Mobile file read pointer, successfully returns the location of the file pointer, failed to return -1
Parameters: Handle: Associated handle; Offset: Moved; Orgin: Mobile Betmark, 0 = File Head, 1 = Current location, 2 = file end.
Example: ShowMessage (Fileseek (HND1, 0, 2)); // Get the length of the file
⑸filewrite ()
Prototype: Extern package int __fastcall filewrite (int Handle, Const Void * Buffer, INT Count);
Function: Write the file, return the number of bytes that actually written, the handle must first be created by FileOpen or FileCreate.
Parameters: Handle: Handle to be written; buffer: Store buffer written in data; Count: Want to write bytes
Example: char str [] = "i love you"; FileWrite (HND1, STR, STRLEN (STR));
⑹filecreate ()
Prototype: extern package int __fastcall filecreate (const onSition filename);
Function: Create a file. Successfully returns its handle, otherwise returns -1
Parameters: filename: The name of the file to create
Example: if (! Fileexists ("kc.c")) HND1 = FileCreate ("kc.c");