From

xiaoxiao2021-03-06  77

Word Source VC World - C language classroom

Self-archiving, also dedicated to the same Delphi programmer as me

File random reading The previous introduction to the read and write mode of the file is sequential, that is, the read and write file can only be read from the beginning, sequential read and write each data. However, in practice, you often ask for a certain specified part in read-only write files. In order to solve this problem, the location pointer inside the mobile file is moved to the location that needs to be read, and then reads and writes, this read and write is called random reading. The key to achieving random reading is to move the position pointer as required, which is called the file positioning. File Location Mobile File Internal Location Pointer There are two, namely the REWIND function and the FSEEK function. The REWIND function has been used several times, and its call form is: Rewind (file pointer); its function is to move the location pointer inside the file to the file. The following mainly introduces the FSEEK function. The FSEEK function is used to move the file internal location pointer, which is: fseek (file pointer, displacement, starting point); where: "File Pointer" points to the mobile file. "Displacement" means that the number of bytes moved, requiring the displacement amount to be long data so that it will not be wrong when the file length is greater than 64kb. When the displacement is indicated by a constant, it is required to add "L". "Start Point" indicates where to start calculating the bitmine, three specified starting points: file first, current location and file end. Its representation is shown in Table 10.2. The starting point represents the symbol number - ──────────────────- Document SEEK-SET 0 Current location SEEK-CUR 1 file end SEEK-END 2 For example: FSeek (FP, 100L, 0); its meaning is to move the position pointer to the first 100 bytes of files. It is also shown that the FSEEK function is generally used for binary files. In the text file, due to the conversion, the location that is often calculated will occur. The random read and write of the file After moving the position pointer, you can read and write any of the read and write functions previously described. Since it is generally read and written a data block, the FREAD and FWRITE functions are commonly used. The following uses an example to explain the random read and write of the file. [Example 10.8] The data of the second student is read in the Student Document STU LIST. #include struct stu {char name [10]; int Num; int Age; char addr [15];} BOY, * QQ; main () {file * fp; char ch; int i = 1; QQ = & boy; if ((fp = fopen ("stu_list", "RB"))) == null) {Printf ("Cannot Open File Strike any key exit!); getCh (); exit (1);} (fp); FSeek (FP, I * SizeOf (Struct Stu), 0); FREAD (QQ, SIZEOF (Struct Stu), 1, FP); Printf ("/ N / NNAME / TNUMBER AGE ADDR / N"); Printf ("% S / T% 5d% 7D% S / N", QQ-> Name, QQ-> Num, QQ-> AGE, QQ-> AddR);} The file STU_LIST has been established by the program 10.6, this The program reads out the data of the second student with a randomly read method. Defining BOY is the STU type variable, QQ is a pointer to BOY. Open the file in a binary file, the program 22th line moves the file position pointer. Where the I value is 1, indicates the length of the STU type from the file header, and then read out is the data of the second student. The file detection function commonly used in the file detection function C language has the following.

First, the file end detection function feOf function call format: FeOf (file pointer); function: Determine if the file is in the file end position, if the file ends, the return value is 1, otherwise 0. Second, read and write file error detection functions FERROR function call format: Ferror (file pointer); function: Check if the file is read or written in various input and output functions. If the FERROR return value is 0, it is wrong, otherwise it means wrong. Third, the file error flag and file end flag set 0 function clearrr function call format: Clearerr (file pointer); function: This function is used to clear the error flag and file end flag to make them zero. The C library file C system provides a wealth of system files, called library files, and C library files are divided into two categories. One type is a file called ".h", called header file, in the previous included command We have used many times. In the ".h" file contains information such as constant definition, type definition, macro definition, function prototype, and various compilation selection settings. Another category is a function library, including a target code for various functions, and is called in the program. When a library function is typically called in the program, you want to include the ".h" file where the function prototype is in the call. A total library function is given in the appendix. Alloc.h Description Memory Management Function (Assign, Release, etc.). AskERT.H Defines an Assert debugging macro. Bios.h Description Call the various functions of the IBM-PC ROM BIOS subroutine. Conio.h describes the various functions of calling the DOS console I / O subroutine. Ctype.h contains a name-class information (such as Isalpha and Toascii, etc.) about character classification and conversion. Dir.h contains structures, macro definitions, and functions related to directory and path. DOS.H defines and describes some constants and functions that MSDOS and 8086 calls. Erron.h defines a mission of the error code. Fcntl.h defines symbol constants when connecting to the Open library. Float.h contains some parameters and functions about floating point operations. Graphics.h describes some of the specific structures of various functions, graphical error codes, and some special structures that are for different colors of different drivers, and functions. IO.H contains the structure and description of the low-level I / O subroutine. Limit.h includes information such as environmental parameters, compilation time limits, number of numbers. Math.h illustrates the mathematical calculation function, and it has also set the HUGE VAL macro to illustrate the special structure used by Matherr and Matherr subroutines. MEM.H illustrates some memory operation functions (most of them are also illustrated in string.h). Process.h Describes the structure of each function, spawn ... and exec ... function of process management. Setjmp.h Defines the JMP BUF type used by longjmp and setjmp functions, indicating these two functions. Share.h defines the parameters of the file sharing function. Signal.h Defines SIG [ZZ (Z] [ZZ)] IGN and SIG [ZZ (Z] [ZZ)] DFL constants, describing both Rajse and Signal. Stdarg.h defines the macro of the read function parameter table. Such as Vprintf, VSCARF functions. STDDEF.H defines some public data types and macros. Stdio.h defines the type and macros defined in the UNIGHAN and RITCHIE in UNIX System V. Standard I / O predefined streams are also defined: stdin, stdout, and stderr, describe the I / O draft program. STDLIB.H illustrates some common subroutines: converted subroutines, search / rank sequence, etc. String.h Description Some string operations and memory operation functions. Sys / stat.h defines some symbol constants used when opening and creating files.

SYS / TYPES.H Describes the FTIME function and the TIMEB structure. Sys / time.h defines Type Time [Z] [ZZ)] T. Time.h Defines Time Converter AscTime, LocalTime, and Gmtime 's Structure, CTIME, DIFFTIME, GMTIME, LOCALTIME, and STIME to be used, and provide prototypes of these functions. Value.h defines some important constants, including some constants that depend on the machine hardware and instructions compatible with UNIX System V, including floating point and double precision values. Summary 1. The C system is used as a "stream" and processed by byte. 2. The C file is divided into binary files and ASCII files by encoding. 3. C language, use the file pointer identifies the file, and the file pointer can be obtained when a file is opened. 4. The file must be opened before reading and writing, and the reading and writing must be turned off. 5. Documents can be opened by read-only, write, read, write, and add four operations, and must also specify the type of files to be binary or text files. 6. Files can be read by bytes, strings, and data blocks, and files can also be read and written in the specified format. 7. The location pointer inside the file indicates the current read and write location, and the pointer can be used to read and write files.

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

New Post(0)