(补 补) Borland C ++ Bulder file operation summary-4

xiaoxiao2021-03-06  66

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 function 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 () Extract Filename () extractfilename () from the file name ExtractFileName () extractfilepath () extractfilepath () from the file name. ExtractFileDir () Extract File Name from the File Name ExtractFileDrive () Extract the drive name ChangeFileExt () change file extensionExt () EXPANDUNCFILENAME () Returns the full path of the file with the network drive ExtractRACTRACTRACTRACTRACTRACTRACTRACTRACTRATIVEPATH () Patted relative path information from the file name ExtractShortPathname () Transforms file names to DOS 8 · 3 format Matchesmask () checks if the file matches the specified file name format

Let's introduce these functions below:

()ExpandFileName () prototype: exTern package anstring __fastcall expandFileName (const anstruing 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 )ExTractFileExt () prototype: extern package ansiString __fastcall extractfileext (const anstruing 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"

DEXTRACTFILENAME () Prototype: Extern package ansiString __fastcall extractFileName (const onSistring 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 ansiString __fastcall extractfilepath (const onSistring 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 anstring __fastcall extractfiledir (const ANSISTRING FILENAME); Function: Different from the file name (different from the previous function, does not include 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

⑹EXTRACTFILEDIVE () prototype: Extern package Ansistring __fastcall extractfiledrive (const anstruing 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 changefileext (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 ansiString __fastcall expanduncfilename (const anstruing 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 ansiString __fastcall extractShortPathName (const ANSISTRING 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 onSistring filename, const ansition mask);

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 functions This type of function includes various operations related to setting and reading the drive, subdirectories, and files, listing the functions commonly used by such operations and their functions.

Function Function CREATEDIR () Creating a new subdirectory deleteFile () Delete file DirectoryExists () Determined Directory Whether to exist DiskFree () Get Disk Retention Space Disksize () Get Disk Capacity FileExists () Determined FilegetTr () Get File Properties Filegetdate ) Get file dates 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 :: aNSISTRING 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);

DIRECTORYEXISTS () 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 this directory does not exist, create a ⑷diskfree () prototype: exTern package __int64 __fastcall diskfree (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;

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 anstring fileEname);

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 filegetttr (const onSistring filene);

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 Fahidden $ 00000002 Implied File Fasystem $ 00000004 System File FAVOLUMEID $ 0000000020 Contributation Fadirectory $ 00000020 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 onswout);

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 This type of function completes the operation of reading and writing files, 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 () Open FileClose () Close File FileRead () Read Fileseek () File Location FileWrite () Write FileCreate () Create File

These functions are described in detail below.

(1) FileOpen () prototype: Extern package int __fastcall fileopen (const anstring filename);

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 Open FMOpenWrite 1 with read-only attribute to open FMOpenReadWrite 2 with read / write properties Open FMShareCompat 0 compatible FCB mode (there is corresponding DOS function call in assembly , I am here, I'm opening the way: Open in an exclusive way, before the closure Reading and writing: 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);

CAPFILEREAD () 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 rt 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 anstruing 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");

转载请注明原文地址:https://www.9cbs.com/read-109826.html

New Post(0)