C language file and its basic operation


foreword

Files are an integral part of today's computer systems. Files are used to store programs, documents, data, letters, tables, graphics, photos, videos, and many other kinds of information. As a programmer, you must be able to write programs that create files and read and write data from files


data flow

First of all, let's understand the concept of data flow. As far as C programs are concerned, bytes are moved in and out from the program . This byte stream is called a stream. Programs interact with data in the form of streams. When reading and writing C language files, the "open file" operation will be performed first. This operation is to open the data stream, and the "close file" operation is to close the data stream.

document

A file refers to a collection of data stored on an external storage medium (which can be a magnetic disk, optical disk, tape, etc.). The operating system manages the data on the external media in the form of files.
When opening a file or creating a new file, a stream is associated with an external file (and possibly a physical device). In order to identify a file, each file must have a file name as a sign to access the file, its general structure

    文件名.扩展名
通常情况下应该包括盘符名、路径、主文件名和文件扩展名4部分信息

The program interacts with the external storage (external storage medium) mainly through the following two methods in the process of running in the memory:

  1. Write data to external memory in units of files .
  2. Read the data in the file from the external storage according to the file name .

That is to say, if you want to read the data in the external storage medium, you must first find the corresponding file according to the file name, and then read the data from the file; Create a file and write data to the file.

The C language supports streaming files, that is, the aforementioned data streams. It regards files as a sequence of bytes and accesses them in units of bytes. There is no record boundary, that is,The start and end of data input and output are only controlled by the program, not by physical symbols (such as carriage return and line feed)

file operation

pointer to file type

typedef struct{
    
    
	short level;         //缓冲区“满”或“空”的程度
	unsigned flags;      //文件状态标志
	char fd;             //文件描述符
	unsigned char hold;  //如无缓冲区则不读取字符
	short bsize;         //缓冲区的大小
	unsigned char *baffer; //数据缓冲区的读写位置
	unsigned char *curp; //指针指向的当前文件的读写位置
	unsigned istemp;     //临时文件,指示器
	short token;         //用于有效性检查
}FILE;

When using a data file, you only need to pre-include the stdio.h header file, and then define a pointer to the structure type, without caring about the details of the FILE structure.

FILE *fp;//直接定义即可

Library functions for file operations

  • File opening
    fopen(): open the file
  • File closing
    fclose(): close the file
  • File read and write
    fgetc(): read a character
    fputc(): write a character
    fgets(): read a string
    fputs(): write a string
    fprintf(): write formatted data
    fscanf() : format read data
    fread(): read data
    fwrite(): write data
  • File status check
    feof(): whether the file is over
    ferror(): whether there is an error in reading/writing the file
    clearerr(): clear the file error flag
    ftell(): the current position of the file pointer
  • File pointer positioning
    rewind(): move the file pointer to the beginning fseek(): reposition the file pointer

1. Open the file

Function prototype :

FILE *fopen(char *filename,char *mode);
//filename:文件名
//mode:访问方式

Function : Open the specified filename file using mode mode. If the file is successfully opened, a FILE pointer is returned; if the file fails to be opened, NULL is returned.

file usage significance
“r” Read-only opens a text file, allowing only data to be read
“w” Open or create a text file for writing only, allowing data to be written only
“a” Append opens a text file and writes data at the end of the file
“rb” Read-only opens a binary file, allowing only data to be read
“wb” Open or create a binary file for writing only, allowing data to be written only
“ab” Append opens a binary file and writes data at the end of the file
“r+” read-write opens a text file, allowing reading and writing
“w+” Read-write opens or creates a text file, allowing reading and writing
“a+” Read and write opens a text file, allowing reading, or appending data at the end of the file
“rb+” read-write opens a binary file, allowing reading and writing
“wb+” read-write opens or creates a binary file, allowing reading and writing
“ab+” Read-write opens a binary file, allowing reading, or appending data at the end of the file

Two ways to open a file read-only:

方法1:
FILE *fp;
fp=fopen("E:\\code\\1.txt", "r");
方法2:
FILE *fp;
fp=fopen("E:/code/1.txt","r");

2. Close the file

Function prototype :

int fclose(FILE *fp);

Function : close the file pointed by the file pointer fp. If it returns 0, it means that the shutdown is successful; if it returns a non-zero value, it means that an error occurred.
In the program, after a file is used, if the file is opened in read mode, there is no need to close the file; but if the write mode is used, the fclose() function must be used to close the file , otherwise it will be placed in the buffer at the end Data cannot be written back to the file, and data loss occurs.
After the file is opened and used, no matter how you read or write the file, you should develop a good habit of closing the file.

3. Single-character read and write files

Single character read and write function
The character read and write function is a read and write function with character (byte) as the unit. One character at a time can be read from or written to the file.

  1. Read single character function fgetc()
    function prototype :
  int fgetc(FILE *fp);
  
  //其意义是从fp所指的文件中读取一个字符并送入ch中。
  FILE *fp;
  ch=fgetc(fp);  

Function : Read the characters in the file position currently pointed by the file pointer fp. After reading, the file pointer will automatically move down one character position. If the file pointer has reached the end of the file, return -1.

  1. Write the prototype of the single character function fputc()
    function :
int fputc(char ch,FILE *fp);

//其意义是把字符a写入fp所指向的文件中
FILE *fp;
fputc('a',fp);

Function : Write the character ch into the file pointed to by the file pointer fp, return the ASCII code of the character when successful, and move the internal position pointer of the file backward by one byte; return EOF (in stdio.h, the symbol constant EOF is equal to -1).

Look here for the rest, hehehe!
C language detailed file operation

Guess you like

Origin blog.csdn.net/The_onion/article/details/121629077