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

xiaoxiao2021-03-06  66

C's-based file operation In ANSI C, the operation of the file is divided into two ways, that is, the flow file operation and I / O file operation, the following introduces it.

I. Flow file operation This method of file operation has an important structure file, file 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 * /

This structure contains the basic properties of the file operation. By the operation of the file, the file is used to perform the pointer of this structure. This file operation is commonly used in the following table function function FOPEN () open flow fclose () shut down flow FPUTC () Write a character to the stream FGETC () Read a character fseek () in the stream to locate the specified character fputs () write string to the stream FGETS () read a line from the stream or specify a character fprintf () Format Output to FSCANF () Returns the true value Ferror () Return true FEERROR () Return true FEERROR () Reset () Reset File Locator to File Remove () Reset File Locator to File Remove () Delete File Fread () The character fWrite () generated by reading 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 prototype: file * fopen (const char * filename, const char * mode), FOPEN implementation three features

To use and open a stream to connect a file and this stream to this stream returns a Filr pointer parameter filename pointing to the file name to be opened, the Mode represents a string that opens the state, and the value is as follows

The string enlighten "R" Opens the file "W" in read-only mode to open the file "A" to open the file "R " in an additional mode to open the file in read / write, if there is no file error "W " to read / Write a way to open a file, such as 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 binary mode thinks it is two characters 0x0d, 0x0a; if in the file Read 0x1b, text mode will consider this is the file ending, that is, the binary model does not process the file, and the text mode will be converted accordingly on the data.

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 is to close file with fopen () (), whose prototype is: int Fclose (file * fp); if successful, return 0, failback returns 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 () Write 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 from 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 typically used in a file in binary mode. The function is positioned to the location specified in the stream. The prototype is int FSeek (file * stream, long offset, int because); if successful returns 0, parameter OFFSET Is the number of mobile characters, imaging is a benchmark, and the value is

Symbol constant value reference position Seek_set 0 File begins seek_cur 1 Currently read-written position Seek_end 2 file tail

Example: FSeek (FP, 1234L, seek_cur); // Put the read-write position from the current position to the current position upward 1234 bytes (L suffix represents long integer)

FSEEK (FP, 0L, 2); // Move read and write position to the file

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 line from the stream or specifying a character, the prototype is char * fgets (Char * S, INT N, FILE * STREAM); read N-1 characters from the stream, unless reading a line, parameters S is to receive a string, if success, returns the pointer of S, otherwise returns NULL.

Example: If the text of the current location of a file is as follows

Love, I Have

But ........

If used

FGETS (str1, 4, file1);

Then start Str1 = "LOV", read 4-1 = 3 characters, and if used

FGETS (str1, 23, file1);

The Str = "Love, I Have" is executed, and one line is read (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. But it is written.

Example: fprintf (fp, "% 2d% s", 4, "hahaha");

9.fscanf () is read from the stream. The prototype is int Fscanf (file * stream, const char * format [, address, ...]); its usage is the same as scanf (), but not from the console Read, but take it from flow.

Example: fscanf (FP, "% D% D", & x, & y);

10.FEOF () detection is that the end of the file is returned, otherwise it returns 0, its prototype is int feed (file * stream);

Example: IF (Feof (fp)) Printf ("has arrived in the file tail");

11.FERROR () prototype is int Ferror (file * stream); return the most recent error code, can be cleared by clearrr (), the prototype of the Clearerr () is Void Clearerr (File * Stream);

Example: Printf ("% D", FERROR (FP));

12.Rewind () Start the current read and write position, 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); the parameter is the file name to be deleted, and successfully returns 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; the parameter PTR is the data saved, and the pointer of the void * can be used Type pointers replace, such as char *, int *, etc.); size is the number of bytes per piece; n is the number of pieces read, if successful, returns the number of blocks actually read (not bytes) This function is generally used in a file in binary mode.

example:

Char x [4230]; file * file1 = fopen ("c: //msdos.sys", "r"); FREAD (x, 200, 12, file1); // Total reading 200 * 12 = 2400 words Festival

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, Void * The pointer can be replaced by 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 number of blocks actually written (not Number of bytes, this function is typically used in a file in binary mode.

example:

Char x [] = "i love you"; fwire (x, 6, 12, fp); // Write 6 * 12 = 72 bytes

Write "i love" 12 times in FP, a total of 72 bytes

15.TMPFile () Its prototype is file * tmpfile (void); generates a temporary file to open with "W B" mode, and return 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 the obtained file name, and return this pointer, Returns NULL if it fails.

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

New Post(0)