During programming, the operation of the file is a frequently used problem. In C Builder, you can use a variety of ways to file a file operation. Here I follow the following sections, you will have a detailed introduction, this is: 1 C based file operation; 3, WinAPI-based file operation; 4, BCB library-based file operation; 5, the operation of special files. This time I first wrote the first one, I will write other parts in my homepage later.壹, C's File Operation In ANSI C, the operation of the file is divided into two ways, namely the streaming file operation and I / O file operation, the following is introduced.
I. Flow file operation This method of file operation has an important structure file, file defined in stdio.h as follows: typedef struct {int level; / * fill / empty level of buffer * / unsigned flags; / * file status flags * / char fd; / * File descriptor * / unsigned char hold; / * ungetc char if no buffer * / int bsize; / * Buffer size * / unsigned char _FAR * buffer; / * Data transfer buffer * / unsigned char _Far * curp; / * Current Active Pointer * / unsigned istemp; / * Temporary File Indicator * / Short token; / * Used for value; / * this is the file object * / file This structure contains files The basic properties of the operation are performed through the pointer of the file. This file operation is often used by the function function of the following table function FOPEN () open flow fclose () shut down flow FPUTC () write a character to the stream FGETC () reads a character fseek () in the stream to the specified character fputs () write string to the stream FGETS () read a line in the stream or specify a character fPrintf () in format output to flow FSCANF ( ) When reading FeOF () to the end of the FEOF (), returns a true value FERROR () returns a true value FERROR () retouch () Reset file locator to the file Remove () Delete File FREAD () Read from the stream Specifying characters fWrite () Write the specified number of characters in the stream TMPFile () Generates a temporary file stream TMPNAM () Generates a unique file name. Let's introduce these functions 1.Fopen () Fopen's prototype is: FILE * FOPEN (Const Char * FileName, Const Char * Mode), FOPEN implements three functions To open a stream to connect a file and this stream to this stream returns a FILR pointer parameter filename points to the file name to be opened, and the mode represents the string of the open state, and the value is as follows, the table string has the meaning "R" Read-only mode Open the file "W" to open the file "A" to open the file "R " in an additional mode to open the file in reading / write, if there is no file error "W " to open the file in reading / write, if there is no File Generation New File A file can be opened in text mode or binary mode. These differences are: Enter the carriage return in text mode as a character '/ n', and the binary mode thinks it is two characters 0x0d, 0x0a; If you read 0x1b in the file, text mode thinks this is a file ending, that is, the binary model does not process the file, and text mode will be converted accordingly in a certain manner. The system default is to open in text mode, you can modify the value of all variable _fmode to modify this setting, such as _fmode = o_text; set the default open mode as text mode; _fmode = o_binary; set the default open mode is binary mode.
We can also specify open mode in the mode string, such as "RB" to open read-only files, "W T" or "WT " in binary mode indicate that the read / write file in text mode is opened. This function returns a FILE pointer, so there is no initialization after a FILE pointer, but uses fopen () to return a pointer and connected to a specific file, if success or failure, return null. Example: File * fp; if (fp = fopen ("123.456", "WB")) PUTS ("Open File Success"); Else Puts ("Open File Beail"); 2.Fclose () Fclose () function That is to turn off files opened with fopen (), whose prototype is: int Fclose (file * fp); if successful, return 0, failback to return EOF. At the end of the program, you must remember to close the open file, otherwise it may cause data loss, and I have often made such a problem. Example: fclose (fp); 3. FPUTC () Writing a character, the prototype is int FPUTC (INT C, File * Stream); successfully returns this character, failed to return EOF. Example: FPUTC ('x', fp); 4.Fgetc () reads a character in the stream, the prototype is int FPUTC (file * stream); successfully returns this character, failed to return EOF. Example: CHAR CH1 = FGETC (FP); 5. fseek () This function is generally used in a file in binary mode. The function is to position the location specified in the stream, the prototype is int FSeek (file * stream, long offset, int If it is successful to return 0, the parameter OFFSET is the number of mobile characters. The imaging is the benchmark of the mobile. The value is the symbol constant value reference position seek_set 0 file begins seek_Cur 1 Currently read-written position SEEK_END 2 file Subth case: fseek (FP , 1234L, seek_cur); // Put the read-write position from the current position to the current position backward 1234 bytes (L suffix represent long integer) FSEEK (FP, 0L, 2); // Move the read and write position to the file end 6.fputs () Write a string into the stream, prototype int FPUTS (Const Char * S, File * Stream); example: fputs ("i love you", fp); 7.FGETS () reads a row in the stream or specified Characters, prototypes are char * fgets (char * s, int N, file * stream); read N-1 characters from the stream unless the parameter S is to receive a string, if success is successful, returns S The pointer, otherwise returns NULL. Example: If the text of a file is as follows, I how but ........ If you use fgets (str1, 4, file1); then execute Str1 = "LOV", read 4-1 = 3 characters, and if Fgets (STR1, 23, File1); execute str = "love, i Have", read a row (not including '/ n').
8.fprintf () Enter the stream in format, its prototype is int FPrintf (file * stream, const char * format [, argument, ...]); its usage is the same as Printf (), but it is not written to the console. It is written for example: fprintf (fp, "% 2d% s", 4, "hahaha"); 9.fscanf () is read from the stream, whose prototype is int Fscanf (File * Street, Const Char * format [, address, ...]); its usage and scanf () are the same, but not from the console, but from the flow reading. Example: fscanf (fp, "% D% D", & x, & y); 10.FeOf () detection has arrived with the file, it is true, otherwise it returns 0, its prototype is int Feof (file * stream); example : If (Feof (fp)) Printf ("has arrived in the file tail"); 11.FERROR () prototype is int Ferror (file * stream); return to the most recent error code, can use clearrr () to clear it, Clearerr The prototype is Void Clearerr (File * stream); example: Printf ("% D", FERROR (FP)); 12.Rewind () Start the current read and write position back to the file, the prototype is void Rewind (file * Stream); In fact, this function is equivalent to FSEEK (FP, 0L, Seek_set); example: Rewind (fp); 12.Remove () Delete file, prototype is int Remove (const char * filename); parameter is the file name to be deleted , Successfully returned 0. Example: Remove ("c: //io.sys"); 13.FREAD () reads the character of the specified number from the stream, the prototype is Size_T FREAD (Void * PTR, SIZE_T SIZE, SIZE_T N, FILE * STREAM); Parameter PTR is the data saved, and the pointer of the void * can be replaced with any type of pointer, such as char *, int *, etc.; Size is the number of bytes per piece; n is the number of reads, if Successful, returning the number of blocks actually read (not bytes), this function is generally used in the two-in-one file. Example: char x [4230]; file * file1 = fopen ("c: //msdos.sys", "r"); FREAD (X, 200, 12, file1); // Total reading 200 * 12 = 2400 One byte 14.fWrite () corresponds to the FREAD, write the specified data in the stream, the prototype is size_t fwrite (const void * ptr, size_t size, size_t n, file * stream); parameter PTR is the data pointer to be written The pointer of the void * can replace any type of pointer, such as char *, int *, etc.); size is the number of bytes per piece; N is the number of blocks to write, if successful, return the actual written block Number (not bytes), this function is generally used in a file in binary mode.
Example: char x [] = "i love you"; fwire (x, 6, 12, fp); // Write 6 * 12 = 72 bytes will write "I love" 12 times in the stream FP, 72 byte 15.TMPFile () Its prototype is file * tmpfile (void); generates a temporary file, open with "W B" mode, and returns this temporary stream pointer if it returns NULL. This file will be automatically deleted at the end of the program. Example: file * fp = tmpfile (); 16.tmpnam (); its prototype is char * tmpnam (char * s); generating a unique file name, in fact, TMPFile () calls this function, parameter S is used to save Get the file name and return this pointer, if it fails, return null. Example: TMPNAM (STR1);
Second, the direct I / O file operation This is another file operation provided by C, which is the processing of the file by direct storage / taking the file, and the above-mentioned streaming file operation is done by the buffer; Flow file operation is performed around a File pointer, and this file operation is performed around the "handle" of a file, what is a handle? It is an integer that is the unique number of marks used to identify a document (in Windows, the concept of handle to all device resources). These files are commonly used functions, these functions and some symbols used in IO.H and FCNTL.H, and the corresponding header file is added when used. Function Description Open () Opens a file and returns its handle Close () Close a handle Lseek () Type the specified position of the file read () block read file WRITE () block EOF () test files End FileLength () The file length rename () Rename file chsize () change the file length The following describes: 1.Open () Opens a file and returns its handle, if it fails, a value of less than 0 will be returned. The prototype is int Open (const char * path, int access [, unsigned mode]); parameter PATH is the file name to open, Access is open mode, Mode is optional. Represents the properties of the file, mainly used in the UNIX system, which is meaningless in DOS / Windows. The open mode of the file is as follows. Symbol Meaning Symbol Meaning O_WRDONLY MET O_RDWR Read / Write O_NDELAY O_CREAT O_APPEND Additional Method O_CREAT If the file does not exist, create o_trunc to cut the file length as 0 O_EXCL and O_CREAT, if the file is returned Error O_BINARY Binary O_Text Text Mode For multiple requirements, you can use the "|" operator to connect, such as o_append | o_text indicates that the file is opened in text mode and append mode. Example: int Handle = Open ("c: //msdos.sys", o_binary | o_creat | o_write) 2.close () Turns off a handle, the prototype is int close (int Handle); if it successfully returns 0: Close (Handle ) 3. Lseek () positioned to the specified location, prototype is: long Lseek (int.comwhere); Parameter OFFSET is the amount of movement, fromwhere is a mobile reference location, the value and the previously speaking FSeek The same, seek_set: file header; seek_cur: file current location; seek_end: File end. This function returns new access location after execution. Example: Lseek (Handle, -1234L, Seek_cur); // Put the access location from the current location 1234 bytes. X = lseek (hnd1, 0l, seek_ek_ek_ek); // Move the access position to the file end, X = The position of the file is the file length 4.Read () reads a piece from the file, the prototype is int ie, Void * buf, unsigned len; parameter BUF Saves read data, LEN is the byte read. The function returns bytes actually read.
Example: CHAR X [200]; READ (HND1, X, 200); 5.Write () Write a piece of data into the file, the prototype is int Write (int Handle, Void * BUF, UNSIGNED LEN); the meaning of the parameters is READ (), Return the byte that is actually written. Example: char x [] = "I love you"; Write (Handle, X, Strlen (x)); 7.eof () is similar to FeOf (), whether the test file end is end, it is returned 1, otherwise returns 0; prototype is : Int EOF (INT HANDLE); case: While (! EOF (Handle1)) {...}; 8.fileLength () Returning the file length, prototype is long filength; equivalent to Lseek (Handle, 0L, Seek_ek_ek_ek Example: Long x = filelength; 9.Rename () Rename file, prototype is int rename (const char * oldname); parameter OldName is an old file name, NewName is a new file name. Successfully returned 0 cases: rename ("c: //config.sys", "c: //config.w40"); 10.chSize (); change the file length, prototype is int CHSIZE (INT HANDE, long size); The parameter size represents the new length of the file, and successfully returns 0, otherwise returns -1, if the specified length is less than the file length, the file is shorted; if the specified length is greater than the file length, then add '/ 0' after the file. Example: Chsize (Handle, 0x12345); ----------------------------------------- --------------------------------------- If you are familiar with compilation, this way can you find this way and compilation Language DOS function call handle file operation is very similar, such as Open () is like the 3ch function call of the DOS service, in fact, this operation has two types of functions are done directly with DOS function, such as _open ), _ Dos_open (), etc. Interested in searching for BCB help. The same flow is the same, which also provides a function of the Unicode character operation, such as _wopen (), etc., used for wide character programming under 9x / NT, interested in the help of the BCB. In addition, such operations are LOCK (), unlock (), locking () or the like for multi-user operation, but not much in BCB, I will not introduce, but if I want to write with C CGI, these are necessary, if you have this requirement, then you have to look good.
In C , there is a stream class, all I / O is based on this "stream" class, including the file I / O we want to know, and Stream has two important operators: 1, insert (<<) outputs data to stream. For example, the system has a default standard output stream (COUT), in general, the display, so cout << "Write stdout" << '/ n'; "Write Stdout" and wrap characters ('/ n') outputs to standard output flow. 2, the parser (>>) inputs data from the stream. For example, the system has a default standard input stream (CIN). In general, the keyboard is referred to, so CIN >> X; represents data that reads a specified type (ie, the type of variable x) from the standard input stream . In C , the operation of the file is implemented by Stream's sub-FSTREAM (File Stream), so use this way to operate files, you must join the header file FStream.h. Here, this type of file operation process will come. First, open the file in the FSTREAM class, there is a member function Open (), which is used to open the file, its prototype is: void Open (const char * filename, int mode, int access); parameter: filename: Open File name Mode: How to open the file access: Open the file's properties Open the file in the way in the class iOS (is the base class of all stream I / O classes), the common value is as follows: ios :: App: Take additional Open the file iOS :: ATE: After the file is turned on, IOS: App will contain this property iOS :: binary: Open the file in a binary, the default approach is text. The difference between the two ways, IOS :: in: file open iOS :: OUT: file in an input method Open iOS :: NOCREATE: No file, so the file does not exist when the failed IOS :: NorePlace: Not overwritten File, so if the file is opened if the file is failed iOS :: trunc: If the file exists, set the file length to 0 can be used to connect the above properties, such as ios :: Out | ios :: binary Open file properties The value is: 0: Normal file, open access 1: read-only file 2: Infinuous file 4: System file can be connected with "or" or " " to connect the above properties, such as 3 or 1 | 2 is read only And implied attributes open files.
For example: Open files c: /config.sys fstream file1; file1.Open ("c: //config.sys", ios :: binary | ios :: in, 0); if the Open function is only file name One parameter is opened by reading / writing ordinary files, namely: file1.Open ("c: //config.sys"); <=> file1.Open ("c: //config.sys", ios :: IN | iOS :: OUT, 0); In addition, FStream also has the same constructor as Open (), for the above example, you can open the file when defined: fstream file1 ("c: //config.sys) "); Specifically, FStream has two subclasses: IFStream (Input File Stream), IFStream defaults to open files by default, and OFSTREAM opens files by default. Ifstream file2 ("c: //pdos.def"); // Open file OFSTREAM file3 ("C: //x.123") in an input mode; // Open file in output, in practice, according to Different, select different classes to define: If you want to open in an input mode, use ifstream to define; if you want to open it in an output mode, use ofstream to define; if you want to open / output, use fstream. To define. Second, the file opened by the shutdown file must be turned off after using the file, and FStream provides the member function close () to complete this, such as file1.close (); turn the file1 connected file. Third, read and write file read and write files are divided into text files and binary files. For text files, it is relatively simple, and it is possible to use inserts and parsers; and for binary reading, it is complex. To introduce these two ways, the reading and writing of the text file is very simple: use the insert (<<) to the file output; use the quilt (>>) from the file input. Suppose File1 is opened in an input, and FILE2 is turned on. Examples are as follows: file2 << "i love you"; // Write the string "I love you" INT i; file1 >> i; // from the file into the file. This approach has a simple formatting ability, such as specifying an output as 16 enrollment, etc., the specific format has the following manipulators function input / output DEC formatter as a decimal numerical data input and output ENDL output a newline character. And refresh this stream output ENDS Output an empty character output HEX formatted to hexadecimal numerical data input and output OCT formatted as an octal numerical data input and output setpxecision (int P) Setting the FET value of the floating point number output, for example, 123 as a hexadecimal output: file1 << HEX << 123; To output 3.1415926 with 5-bit precision: file1 << setpxecision (5) << 3.1415926.
2, the reading and writing of the binary file 1Put () PUT () function writes a character to the stream, whose prototype is OFSTream & Put (Char CH), is also simple to use, such as file1.put ('c'); A character 'c'. 2Get () GET () function is flexible, there are three commonly used overload form: one is the form of PUT (): ifstream & Get (CHAR & Ch); function is to read a character from the stream, and save it Quote CH, if you go to the file, return empty characters. Such as file2.get (x); indicates that a character is read from the file and saves the read characters in X. Another prototype of the overloaded form is: int GET (); this form is to return a character from the stream, if the file is reached, returns EOF, such as x = file2.get (); and the above example function is the same . There is also a form of prototype: ifstream & get (char * buf, int num, char delim = '/ n'); this form reads characters into arrays pointed to by BUF until the NUM characters are read or encountered. The characters specified by Delim, if Delim is not used, the default quotation '/ n' will be used. For example: file2.get (str1, 127, 'a'); // read characters from the file to the string STR1, termination when the character 'a' is encountered or read 127 characters. 3 Read and write data blocks To read the binary data block, use the member function read () and write () member functions, their prototypes are as follows: read (unsigned char * buf, int num); Write (const unsigned char * buf, int Num ); Read () reads NUM characters from the file to the cache pointing to the BUF, if the file end is reached when NUM characters are not read, you can use the member function int gcount (); to get the actual read. The number of characters; and Write () writes NUM characters from the BUF to the NUM character to the file, it is worth noting that the type of cache is unsigned char *, sometimes the type conversion may be required. Example: unsigned char str1 [] = "i love you"; int N [5]; ifstream in ("xxx.xxx"); OFSTREAM OUT ("YYY.YYYYY"); Out.write (str1, strlen (str1) ); // write all strings STR1 all in YYY.YYY in. Read ((unsigned char *) n, sizeof (n)); // Read the specified integer from XXX.xxx, pay attention to the type conversion IN. Close (); out.close (); 4, detect EOF member function EOF () is used to detect whether to reach the file, if the end of the file returns a non-0 value, otherwise returns 0. The prototype is int EOF (); example: if (in.eof ()) showMessage ("I have arrived in the file!"); V. File positioning and C file operation method, C I / O system management two Pointers associated with a file. One is a read pointer, which indicates that the input is operated in the file; the other is the write pointer, which is the next time the next time. The corresponding pointer automatically changes each time the input or output is performed.
Therefore, the C file position is divided into the positioning of the read position and the write position. The corresponding member function is SEEKG () and seekp (), and seekg () is setting the read position, and the Seekp is set. Their most general form as follows: istream & seekg (streamoff offset, seek_dir origin); & seekp (streamoff offset, seek_dir origin) ostream; streamoff defined in iostream.h, the definition has a maximum offset offset that can be achieved, The seek_dir represents The moving reference position is an enumeration with the following value: ios :: beg: Fair ios :: cur: file Current position ios :: End: File end These two functions are generally used for binary, because text files Because the system can be different from the interpretation of characters. Example: file1.seekg (1234, iOS :: Cur); // Put the read pointer of the file from the current position 1234 bytes file2.seekp (1234, ios :: beg); // Write the write pointer Remove 1234 bytes from the beginning of the file ----------------------------------------------------------------------------------------------------------------- --------------------------------------- With this knowledge, we can complete The operation of the file, of course, there is still a lot of member functions I have not introduced, but these we have completed most of them, this way of operation is a method I prefer, flexible than C. It is versatile than BCB functions and WinAPI functions. Next time, I will introduce the library function of the file operation provided by the BCB. 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 matchsmask () Check if the file matches the files with the specified file name format: (1) Expern Package Ansistring __fastcall expandFileName (Const Ansistring FileName); Features: Return to the full path (including drive, path) Parameters: filename: The file name to process: showMessage (Application-> Exename)); // Show your program file name, such as C: /MyBCB/Sample1.EXE ⑵ExtractFileExt () prototype: extern PACKAGE AnsiString __fastcall ExtractFileExt (const AnsiString fileName); function: extracting parameters from the extension of the file name: fileName: name of the file to be processed (full path) Example: ShowMessage (ExtractFileExt ( Application-> Exename)); // Display ".exe" () gextractFileName () prototype: Extern package anstring __fastcall extractFileName (const ANSISTRINGENAME); Function: Patting the file name parameters that do not contain the path from the file name: FileName: To process file name Example: ShowMessage ("c: //winnt//sol.exe")); // Displays "Sol.exe" ⑷ExtractFilePath () prototype: Extern package Ansistring __fastcall extractFilepath (const ANSISTRING FileName); Features: From the file name Path Name Parameters: FileName: The file name to be processed: showMessage ("Winnt // Sol.exe")); // Display "Winnt /" ⑸EXTRACTFILEDIR () prototype: Extern package Ansistring __fastcall extractfiledir (Const Ansistring FileName); Features: Different from the file name (different from the last function, do not include the last "/") parameter: filename: The file name to process: showMessage ("Winnt // Sol.exe" ));//display"
Difference ⑹ExtractFileDrive Winnt ", attention and the functions of () Prototype: extern PACKAGE AnsiString __fastcall ExtractFileDrive (const AnsiString FileName); Function: filename embodiment to be addressed:: extracting drive name parameter from the file name: FileName 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 for the file name this string: filename: The name of the file to be renamed: new extension: showMessage ("ChangefileExt C: //winnt//sol.exe "," .ooo ")); // Displays" C: /Winnt/Sol.ooo "⑻ExpanduncFileName () prototype: exTern package anstring __fastcall expanduncfilename (const anenstring filename); function: Returns the full path of the network drive, the format is: // machine name / shared name / file name parameter: filename: SHOWMESAGE ("f: //winnt//sol.exe") ); / * if F: is mapped network drive // NT40 / WINNT, displays "//NT40/WINNT/SOL.EXE"*/ ⑼ExtractRelativePath () prototype: extern PACKAGE AnsiString __fastcall ExtractRelativePath (const AnsiString BaseName, const AnsiString Destname); Function: Pumping from the file name Take the relative path information, such as "../ss/ss.asd" form parameters: baseName: Benchmark file name; destname: target file name: showMessage ("D: //source//c//1.123 "," D: //source//ASM//dz.asm ")); / * Display" ../ASM/dz.asm"*/ ⑽Extractshortpathname () prototype: exTern package ansiString __fastcall extractShortPathName (const onging filename) ; Function: Convert file names to DOS 8, 3 format parameters: filename: File name to process: showMessage ("E: // program files // dual whean mouse // 4dmain.exe");
/ * Display "E: /PROGRA ~ 1/Dualwh ~ 1/4dmain.exe" * / ⑾matchesmask () prototype: Extern package bool __fastcall matchesmask (const aion mask); Features: Check if the file is with the specified file Matching parameters: filename: File name to be processed; Mask: File name format, support Wongci: showMessage ("lxf.exe", "*.? X?); // Display" True "- -------------------------------------------------- ---------------------------- 2, file management functions This type of function includes setting and reading drive, subdirectory, and file related Various operations, the following table lists the common functions of 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 Gets the Document Date GetCurrentDir () Get the current directory RemoveDir () Delete Directory setCurrentDir () Sets the current directory to introduce these functions: ()createdIR () prototype: Extern package bool __fastcall createDir (const system :: Ansistring Dir); Function: Create a subdirectory, if you successfully return true, return false parameters: Dir: The name of the subdirectory to be established: CREATE ("ASM"); // Create a subdirectory called ASM in the current directory (2) Deltefile ( Prototype: EXTERN PACKAGETEFILE (const system :: answer filename); Features: Delete file, if you successfully return true, otherwise return false parameters: filename: File name to delete: if (OpenDialog1-> execute ()) DeleteFile (OpenDialog1-> FileName); ⑶DirectoryExists () prototype: extern PACKAGE bool __fastcall DirectoryExists (const System :: AnsiString Name); function: to detect the existence of a directory, returns true if so, false otherwise parameters: Name: to detect directory CREATEDIR ("ASM"); // If ASM is not existing, it is created, and it is created. DiskFree () prototype: Extern package __int64 __fastcall diskfree (Byte Drive); Features: Detect disks The remaining space, the return value is in bytes, if The specified disk is invalid, returned -1 parameter: drive: The code of the disk, 0 means the current disk, 1 = a, 2 = B, 3 = C In this case: showMessage (0)); // Display the current disk Surplus Space ⑸DISKSIZE () prototype: extern package __int64 __fastcall disksize (Byte Drive); Features: Detect disk capacity, return value in byte, if the specified disk is invalid, return -1 parameters: drive: Disk code, 0 Represents the current disk, 1 = a, 2 = b, 3 = c In this case: showMessage (DiskFree (0)); // Display the capacity of the current disk ⑹ELEEXISTS () prototype: Extern package bool __fastcall fileexists (const anstring filename) ; Function: Detecting the file exists,
If there is a returning true, return false parameters: filename: File name to be detected: IF (FileExists ("aaa.asm")) deletefile ("aaa.asm"); ⑺filegetttr () prototype: Extern package int __fastcall Filegetattr Const Ansistring FileName); Function: File Properties, if the error returns -1 return value as follows, if returned $ 00000006 Represents a file with implicit and system properties (4 2) constant value, FAHIDDEN 00000002 Implied file fasystem $ 00000004 System file Favolumeiid $ 00000008 Volume Fadirectory $ 00000010 Directory Faarchive $ 00000020 Archive ") SHOWMESAGE (" This is a file with hidden attributes "); There is FileSetattr (), please review the help system ⑻filegetdate () prototype: Extern package int __fastcall filegetdate (int Handle); Function: Return to the File Establishment Time to the second number of seconds: Handle: use FileOpen () open file handle. Example: INT i = fileopen ("c: //autoexec.bat", fmopenread); showMessage (FileGetDate (i)); fileclose (i); FileSetDate (), please review help system ⑼getcurrentdir () prototype: extern PACKAGE AnsiString __fastcall GetCurrentDir (); function: get the current directory name Example: ShowMessage (GetCurrentDir ()); ⑽RemoveDir () prototype: extern PACKAGE bool __fastcall RemoveDir (const AnsiString Dir); function: delete a directory, if successful return True, otherwise returning false parameters: Dir: Directory to be deleted: IF (DIECTORYEXISTS ("ASM")) Removedir ("ASM"); ⑾SetCurrentDir () prototype: Extern package bool __fastcall setcurrentdir (const anstruing dir); function: Set the current directory, if you returned true, return false parameters: Dir: To switch to the directory name: setCurrentDir ("c: // windows"); --------------- -------------------------------------------------- --------------- 3, the file I / O function This type of function completes the read and write related to the file, this type of operation and C based I / O file operation Similar The following table lists the common functions of such operations and their functions.
FileOpen () Open FileClose () Close File FileRead () Read Fileseek () File Location FileWrite () Write FileCreate () Create File The following is a detailed introduction to these functions. (1) FileOpen () prototype: Extern package int __fastcall fileopen; function: Open the file, if the file is successfully returned, otherwise returns -1 parameter: filename: The file name to open; mode: Open mode, Value is as follows, or "" 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, 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: Options To close: 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 be created by FileOpen or FileCreate. Parameters: Handle: To read the handle; buffer: Buffer that is read; count: Want to read the byte number: 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 parameter: handle: associated handle; Offset: Mobile Quantity; Orgin: Mobile Betmark, 0 = File Head, 1 = Current location, 2 = file end.