File and IO operation in C (1)

xiaoxiao2021-03-06  50

What is a file? "File" is a unit that stores information exchange information inside and outside the computer refers to a collection of data stored on the external medium, whether it is a character or thousands of records, to be stored and processed in a file. In memory, all files must be stored in binary forms, and binary files can communicate directly with memory without conversion. The C language looks a file to make a byte sequence, which consists of a series of bytes, called "stream stream", access in bytes, and has no recorded boundaries.

File Category: First, the media supplied according to the file: disk file, tape file, memory file, device file. Second, according to the content: source program file, target file, data file. Third, press the operating system to read and write the disk file: buffer file system and non-buffer file system. Buffer file system: Operating system opens up a read and write buffer for each file in memory. Fourth, press the data organization form: ANSII code file and binary file ansii file: also called text file, each byte stores an ANSII code, represents a character. Output with the ANSII code form corresponds to the character, one byte represents a character, so it is easy to process characters one by one, which is also easy to output characters. However, generally accounted for many storage space, and spend conversion time (transition between binary and ANSII). Binary file: Put the data in the file in the stored form in memory to the magnetic disk. Advantages: The occupied storage space is small, the file form and memory form are consistent, and no conversion is required, and thus the speed is fast.

File Type Pointer: To call a file, you must know the information related to this file, such as file name, file status, file current read and write position, with the memory buffer address corresponding to the file, the buffer unprocessed string , File operation mode and other information, which is stored in a structural variable, which is defined by the system, named file. Typedef struct {short level; // buffer 'full' or 'empty' degree Unsigned flags; // file status flag char fd; // file descriptor file number unsigned char hold; // If there is no buffer area is not read Character short bsize; // buffer size unsigned char * buffer; // data buffer location unsigned char * curp; / / pointer, current point to unsigned istemp; // This file is a temporary file Short token; // Validity check} file; when processed a file, you need to define a file type pointer to create a pointer variable of a FILE type, which is used to point to a File type structure in the system memory (ie File information area). When the program starts running, the system automatically opens three standard files: standard input, standard output, and standard error output, usually the three files are connected to the terminal. Therefore, we used the terminal input or output we used to open the terminal file. The system also defines three file pointers: stdin, stdout, stderr points to them.

Usage in Example: First, Fopen (), Fclose (), FGETC (), FPUTC (): #include #include main () {file * fp; char ch, FileName [20]; SCANF ("% s", filename); / * file * fopen (const char * filename, const char * mode); 1, filename: file name, you can include logical drives, paths, file names and files extension name. Second, MODE: Opening mode "W" If there is no file, it is established; otherwise delete the original from new establishment. "a" opens the original file, add data. "A ", "W ", "R " mode open files can enter data, or output data is added to b is binary, file *: return value, a file type pointer variable * / fp = fopen (filename, "a"); if (fp == null) {Printf ("/ ncan not open file / n"); exit (0); // Turn off all files, terminate the executed program} Printf ("OpenFile / N "); / * ch = getChar (); while (ch! = '#') // If you entered" # ", end {FPUTC (CH, FP); // Putchar (CH); ch = GetChar ();} * / fclose (fp); Return 0;} / * fputc (ch, fp); // Put Character CH Output to disk file FP PUTCHAR () function is derived from FPUTC (), There is such definition #DEfine Putchar (c) in the header file of stdio.h, is it a macro command in a preprocessing command (still remember?), And stdout is the system-defined file pointer variable Its role is to output C to the terminal.

* / / * Copy a disk file to another disk file * / # include #include main () {file * in, * out; char infile [20], Outfile [20]; Printf ("Please enter destination file name: / n"); scanf ("% s", infile); Printf ("Please enter the output file name: / n"); scanf ("% s" , outfile; IN = fopen (Infile, "A "); if (in == null) {printf ("/ n output file can not be opened / N"); exit (0); // Turn off all files, terminate Executed program} IF (out = fopen (outfile, "a ")) == null) {printf ("/ n destination file can not be opened!"); Exit (0);} Printf ("Kaishiduxie / N" ); While (! feof (out) {printf ("zhengzaiduexie / n"); Printf ("Enter"); FPUTC (FGETC (OUT), IN);} fclose (in); fclose (OUT); return 0;} II. Use of FREAD () and fwrite (): Use FGetc and FPUTC functions to read and write a character in writing. However, a set of data is often required to read a set of data (for example, a real number or a variable value), and the ANSI C standard is proposed to set two functions FREAD and FWRITE to read and write a data block.

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

New Post(0)