BCB-based file operations

Source: Internet
Author: User

BCB also provides file operation functions. These functions are roughly the same as previously described. However, these functions are closely related to BCB and can use AnsiString and other data types in BCB, this method is most convenient for file operations in BCB. I will introduce this file operation in detail below.

The file operation functions provided by BCB can be divided into three types: 1. File Name functions, 2. file management functions, and 3. file I/O functions.

1. File Name Function
The file name function can operate on the file name, subdirectory, drive, and extension. The following table lists these functions and their functions.

Function Description
ExpandFileName () returns the full path (including the drive and Path) of the file)
ExtractFileExt () extracts the extension from the file name
ExtractFileName () extracts non-path file names 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 file extension
ExpandUNCFileName () returns the full path of the file containing the network drive.
ExtractRelativePath () extracts relative path information from the file name
Extractdomainpathname () converts the file name to the DOS 8-3 format.
MatchesMask () checks whether the file matches the specified file name format

The following describes these functions one by one:

(1) ExpandFileName ()
Prototype: extern PACKAGE AnsiString _ fastcall ExpandFileName (const AnsiString FileName );

Function: returns the full path (including the drive and Path) of the file)

Parameter: FileName: name of the file to be processed

Example: ShowMessage (ExpandFileName (Application-> ExeName); // display the program file name, such as C:/MyBCB/Sample1.EXE

(2) ExtractFileExt ()
Prototype: extern PACKAGE AnsiString _ fastcall ExtractFileExt (const AnsiString FileName );

Function: Extract an extension from a file name.

Parameter: FileName: name of the file to be processed (full path)

Example: ShowMessage (ExtractFileExt (Application-> ExeName); // display ". exe"

(3) ExtractFileName ()
Prototype: extern PACKAGE AnsiString _ fastcall ExtractFileName (const AnsiString FileName );

Function: extracts files without paths from file names.

Parameter: FileName: name of the file to be processed

Example: ShowMessage (ExtractFileExt ("c: // Winnt // SOL. EXE"); // display "SOL. EXE"

(4) ExtractFilePath ()
Prototype: extern PACKAGE AnsiString _ fastcall ExtractFilePath (const AnsiString FileName );

Function: extract the path name from the file name.

Parameter: FileName: name of the file to be processed

Example: ShowMessage (ExtractFilePath ("Winnt // SOL. EXE"); // display "Winnt /"

⑸ ExtractFileDir ()
Prototype: extern PACKAGE AnsiString _ fastcall ExtractFileDir (const AnsiString FileName );

Function: extract the directory name from the file name (unlike the previous function, excluding the last "/")

Parameter: FileName: name of the file to be processed

For example, ShowMessage (ExtractFileDir ("Winnt // SOL. EXE"); // display "Winnt". Note the difference with the previous function.

⑹ ExtractFileDrive ()
Prototype: extern PACKAGE AnsiString _ fastcall ExtractFileDrive (const AnsiString FileName );

Function: extract the drive name from the file name

Parameter: FileName: name of the file to be processed

Example: ShowMessage (ExtractFileDrive ("c: // Winnt // SOL. EXE"); // display "c :"

⑺ ChangeFileExt ()
Prototype: extern PACKAGE System: AnsiString _ fastcall ChangeFileExt (const
System: AnsiString FileName, const System: AnsiString Extension );

Function: Modify the file name extension. It is not used to rename a real file, but to process the file name string.

Parameter: FileName: name of the file to be renamed, Extension: New Extension

Example: ShowMessage (ChangeFileExt ("c: // Winnt // SOL. EXE", ". OOO"); // display "c:/winnt/SOL. OOO"

⑻ ExpandUNCFileName ()
Prototype: extern PACKAGE AnsiString _ fastcall ExpandUNCFileName (const AnsiString FileName );

Function: returns the full path of the file containing the network drive. Format: // machine name/shared name/file name

Parameter: FileName: name of the file to be processed

Example: ShowMessage (ExpandUNCFileName ("F: // Winnt // SOL. EXE ");/* If F: Is the mapped network drive // NT40/WINNT," // NT40/WINNT/SOL. EXE "*/

⑼ ExtractRelativePath ()
Prototype: extern PACKAGE AnsiString _ fastcall ExtractRelativePath (const AnsiString BaseName, const AnsiString DestName );

Function: Extracts relative path information from the file name, for example, "../sss/ss. asd ".

Parameter: BaseName: Base File Name; DestName: target file name

Example: ShowMessage (ExtractRelativePath ("D: // Source/c // 1.123", "D: // Source // Asm // dz. asm ");/* display ".. /asm/dz. asm "*/

⑽ Extractaskpathname ()
Prototype: extern PACKAGE AnsiString _ fastcall extract?pathname (const AnsiString FileName );

Function: converts a file name to the 8 or 3 DOS format.

Parameter: FileName: name of the file to be processed

Example: ShowMessage (extractw.pathname ("E: // Program Files // Dual Wheel Mouse // 4dmain.exe");/* display "E:/Progra ~ 1/dualwh ~ 1/4dmain.exe "*/

⑾ MatchesMask ()
Prototype: extern PACKAGE bool _ fastcall MatchesMask (const AnsiString Filename, const AnsiString Mask );

Function: checks whether the file matches the specified file name format.

Parameter: FileName: name of the file to be processed; Mask: file name format, supports wildcards

Example: ShowMessage (MatchesMask ("Lxf.exe ","*.? X ?)); // Display "true"

--------------------------------------------------------------------------------

2. File Management Functions
These types of functions include various operations related to setting and reading drives, subdirectories, and files. The following table lists the common functions and functions of these operations.

Function
CreateDir () creates a new subdirectory
DeleteFile () delete an object
DirectoryExists () determines whether a directory exists
DiskFree () to obtain the remaining disk space
DiskSize () Get disk capacity
FileExists () to determine whether a file exists
FileGetAttr () Get File Attributes
FileGetDate () Get File date
GetCurrentDir () to get the current directory
RemoveDir () delete directory
SetCurrentDir () sets the current directory

The following describes these functions one by one:

(1) CreateDir ()
Prototype: extern PACKAGE bool _ fastcall CreateDir (const System: AnsiString Dir );

Function: Create a subdirectory. If the subdirectory is successfully created, true is returned. Otherwise, false is returned.

Parameter: Dir: name of the subdirectory to be created

Example: Create ("ASM"); // Create a subdirectory named ASM under the current directory

(2) DeleteFile ()
Prototype: extern PACKAGE bool _ fastcall DeleteFile (const System: AnsiString FileName );

Function: delete an object. If the object is successfully deleted, true is returned. Otherwise, false is returned.

Parameter: FileName: name of the file to be deleted

Example: if (OpenDialog1-> Execute () DeleteFile (OpenDialog1-> FileName );

(3) DirectoryExists ()
Prototype: extern PACKAGE bool _ fastcall DirectoryExists (const System: AnsiString Name );

Function: checks whether a directory exists. If yes, true is returned; otherwise, false is returned.

Parameter: Name of the directory to be checked

Example: if (! DirectoryExists ("ASM") CreateDir ("ASM"); // If the directory ASM does not exist, create it

(4) DiskFree ()
Prototype: extern PACKAGE _ int64 _ fastcall DiskFree (Byte Drive );

Function: detects the remaining disk space. The returned value is in bytes. If the specified disk is invalid,-1 is returned.

Parameter: Drive: disk code. 0 indicates the current disk, 1 = A, 2 = B, 3 = C, and so on.

Example: ShowMessage (DiskFree (0); // display the remaining space of the current Disk

⑸ DiskSize ()
Prototype: extern PACKAGE _ int64 _ fastcall DiskSize (Byte Drive );

Function: detects disk capacity. The returned value is in bytes. If the specified disk is invalid,-1 is returned.

Parameter: Drive: disk code. 0 indicates the current disk, 1 = A, 2 = B, 3 = C, and so on.

Example: ShowMessage (DiskFree (0); // display the current disk capacity

Specified FileExists ()
Prototype: extern PACKAGE bool _ fastcall FileExists (const AnsiString FileName );

Function: checks whether a file exists. If yes, true is returned; otherwise, false is returned.

Parameter: FileName: name of the file to be checked

Example: if (FileExists ("AAA. ASM") DeleteFile ("AAA. ASM ");

⑺ FileGetAttr ()
Prototype: extern PACKAGE int _ fastcall FileGetAttr (const AnsiString FileName );

Function: Get file attributes. If an error occurs,-1 is returned.

The returned values are as follows. If $00000006 is returned, it indicates a file with implicit and system attributes (4 + 2)

Constant Value Meaning
FaReadOnly $00000001 read-only files
FaHidden $00000002 implicit File
FaSysFile $00000004 system files
FaVolumeID $00000008 Scale
FaDirectory $00000010 directory
FaArchive $00000020 archive files

Example: if (FileGetAttr ("LLL. TXT") & 0x2) ShowMessage ("this is a file with implicit attributes ");

There is FileSetAttr () corresponding to this. Please refer to the Help System

Using FileGetDate ()
Prototype: extern PACKAGE int _ fastcall FileGetDate (int Handle );

Function: returns the number of seconds from File Creation Time to 0 on January 1 ,.

Parameter: 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 check the help system yourself

⑼ GetCurrentDir ()
Prototype: extern PACKAGE AnsiString _ fastcall GetCurrentDir ();

Function: Get the current directory name

Example: ShowMessage (GetCurrentDir ());

Incluremovedir ()
Prototype: extern PACKAGE bool _ fastcall RemoveDir (const AnsiString Dir );

Function: delete a directory. If the directory is successfully deleted, true is returned. Otherwise, false is returned.

Parameter: Dir: name of the directory to be deleted

Example: if (DiectoryExists ("ASM") RemoveDir ("ASM ");

Export SetCurrentDir ()
Prototype: extern PACKAGE bool _ fastcall SetCurrentDir (const AnsiString Dir );

Function: sets the current directory. If the directory is successfully set, true is returned. Otherwise, false is returned.

Parameter: Dir: name of the directory to be switched

Example: SetCurrentDir ("C: // WINDOWS ");

--------------------------------------------------------------------------------

3. file I/O functions
These types of functions perform read and write operations on files. These types of operations are similar to those of C Based on I/O files. The following table lists the common functions and functions of these operations.

FileOpen () open a file
FileClose () close the file
FileRead () read files
FileSeek () File Location
FileWrite () Write File
FileCreate () create a file

The following describes these functions in detail.

(1) FileOpen ()
Prototype: extern PACKAGE int _ fastcall FileOpen (const AnsiString FileName, int Mode );

Function: Open the file. If the file is successfully opened, its handle is returned. Otherwise,-1 is returned.

Parameter: FileName: name of the file to be opened; Mode: Method of opening. The values are as follows, and can be connected by the "or" ("|") operator.

Constant Value description
-------------------------------------------------------------
FmOpenRead 0 is opened as a read-only attribute
FmOpenWrite 1 open with write-only attribute
FmOpenReadWrite 2 is enabled with the read/write attribute
Fm1_compat 0 is compatible with the FCB method (the compilation has corresponding DOS Function calls, and you are interested in checking the relevant information)
Fm1_exclusive 16 Shared Mode: open in exclusive mode. Others cannot access the shared mode before it is disabled.
FmShareDenyWrite 32: Write Access Denied
FmShareDenyRead 48: Read Access Denied
FmShareDenyNone 64 Shared Mode: unrestricted, read/write allowed
For example, int I = FileOpen ("C: // WINDOWS // Win. ini", fmOpenReadWrite | fm1_exclusive );

(2) FileClose ()
Prototype: extern PACKAGE void _ fastcall FileClose (int Handle );

Function: Disable the opened handle.

Parameter: Handle to be closed

Example: FileClose (I );

(3) FileRead ()
Prototype: extern PACKAGE int _ fastcall FileRead (int Handle, void * Buffer, int Count );

Function: reads a file and returns the actual number of bytes read. The handle must be created by FileOpen or FileCreate.

Parameter: Handle to be read; Buffer: Buffer for storing read data; Count: number of bytes to be read

Example: char str [400]; FileRead (hnd1, str, 400 );

(4) FileSeek ()
Prototype: extern PACKAGE int _ fastcall FileSeek (int Handle, int Offset, int Origin );

Function: Move the File Read pointer and return the position of the file pointer.-1 is returned if the pointer is not moved.

Parameter: Handle: associated Handle; Offset: volume of movement; Orgin: Moving benchmark, 0 = File Header, 1 = current location, 2 = file tail.

Example: ShowMessage (FileSeek (hnd1,); // obtain the object Length

Using FileWrite ()
Prototype: extern PACKAGE int _ fastcall FileWrite (int Handle, const void * Buffer, int Count );

Function: writes a file and returns the number of bytes actually written. The handle must be first created by FileOpen or FileCreate.

Parameter: Handle to be written; Buffer: Buffer for storing written data; Count: number of bytes to be written

Example: char str [] = "I Love You"; FileWrite (hnd1, str, strlen (str ));

Using FileCreate ()
Prototype: extern PACKAGE int _ fastcall FileCreate (const AnsiString FileName );

Function: create a file. Return the handle. Otherwise, return-1.

Parameter: FileName: name of the file to be created

Example: if (! FileExists ("KC. C") hnd1 = FileCreate ("KC. C ");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.