When programming in C ++, code file operations are a common problem. In C ++ Builder, you can use multiple methods to operate files, in C ++ Builder, we will introduce this in detail in the following sections. The following will be a one-to-one list:
1. C-based file operations;
2. File Operations Based on C ++;
3. WINAPI-based file operations;
4. File Operations Based on the BCB library;
5. Special file operations.
In ANSIC, file operations are divided into two methods: stream file operations and I/O file operations, which are described below.
1) stream File Operations
This FILE operation has an important structure FILE, which is 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 validity checking */
- } FILE; /* This is the FILE object */
The C ++ Builder structure contains the basic attributes of file operations. Operations on files must be performed through the pointer of this structure. Common functions for such file operations are as follows:
- Fopen () Open stream
-
- Fclose () Close the stream
-
- Fputc () writes a character to the stream
-
- Fgetc () reads a character from the stream
-
- Fseek () locates the specified character in the stream
-
- Fputs () writes a string to a stream
-
- Fgets () reads a row or a specified character from the stream.
-
- Fprintf () Outputs Data to the stream in the format
-
- Fscanf () reads data from the stream in the format
-
- Returns the true value when feof () reaches the end of a file.
-
- Returns the value of ferror () When an error occurs.
-
- Rewind () resets the file locator to the beginning of the file
-
- Remove () delete an object
-
- Fread () reads a specified number of characters from a stream
-
- Fwrite () writes a specified number of characters to the stream.
-
- Tmpfile () generates a temporary file stream
-
- Tmpnam () generates a unique file name
- Introduction to C ++
- Summary Notes on learning and exploring C ++ library functions
- Basic Conception and method of C ++ Class Library Design
- Does C ++ really have market value?
- Basic Conception and method of C ++ Class Library Design