(补课) Borland C ++ Bulder file operation summary-2

xiaoxiao2021-03-06  63

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 that the system is used to identify a file (in Windows, the concept of handle to all devices). 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 () Number of file length rename () rename file chsize () change the file length

Here, these functions will be described:

1.Open () opens a file and returns its handle, if it fails, the value of less than 0 is returned, the prototype is int open (const char * path, int access [, unsigned mode]); parameter PATH is to open File name, Access is open mode, Mode is optional. Represents the properties of the file, mainly used in the UNIX system, there is no meaning 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 method o_text text method

For multiple requirements, you can use the "|" operator to connect, such as O_APpend | O_Text represents the file in text mode and append mode.

Example: int Handle = Open ("c: //msdos.sys", o_binary | o_creat | o_write)

2.close () Close a handle, the prototype is int close (int Handle); if you successfully return 0

Example: Close (Handle)

3. Lseek () positioned to the specified location, prototype is: long Lseek (int.comwhere); Parameter OFFSET is the amount of mobile, 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 to 1234 bytes. X = lseek (hnd1, 0l, seek_ek_ek); // Move the access position to the end of the file, X = the position of the file is the length of the file

4. Read () reads a piece from the file, the prototype is int READ (INT HANDLE, VOID * BUF, UNSIGNED LEN); the parameter BUF saves the read data, and 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 to the file, the prototype is int Write (int Handle, Void * BUF, UNSIGNED LEN); the meaning of the parameter is READ (), returns 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 it returns 0; the prototype is: int EOF (INT HANDLE);

Example: While (! EOF (Handle1)) {...};

8.FileLength () Returns the length of the file, the prototype is long filength; it is equivalent to Lseek (Handle, 0L, seek_end)

Example: long x = fileLength;

9.Rename () Rename file, prototype is int Rename (const char * newname); parameter OldName is old file name, NewName is a new file name. Success return 0

Example: Rename ("c: //config.sys", "c: //config.w40");

10.chsize (); change the length of the file, the prototype is int CHSize (INT HANDLE, long size); parameter size represents the new length of the file, successfully returns 0, otherwise returns -1, if the specified length is less than the file length, then the file is Trunction; if the specified length is greater than the file length, then add '/ 0' after the file.

Example: chsize (handle, 0x12345);

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

If familiar with compile may find that this method and assembly language DOS function call sentence handle file operation is like, such as Open () is like the 3ch function call of the DOS service, in fact, this operation has two types of functions are direct Finished 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.

Here, I will introduce C-based file operations. The next article will introduce the C file operation, so stay tuned.

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

New Post(0)