In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to open and close C language files". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Foreword:
Is the file operation important?
Of course it's important: if we want our programs to have memory, we need to store the information and data we want to remember in an external file so that we can retrieve it next time.
What is the document?
The files on disk are files. But in programming, we generally talk about two kinds of documents: program files and data files.
Program files: include source program files (suffix .c) and target files (suffix .obj) executable programs (windows environment suffix .exe).
Data file: the content of the file is not necessarily the program, but the data read and written when the program is running, such as the file from which the program needs to read the data, or the file from which the content is output.
The input and output of the data processed on the VS compiler take the terminal as the object, that is, the input data is input from the keyboard of the terminal, and the running results are displayed on the display. But sometimes we will output the data to the disk, and then read the data from the disk to memory when needed. Here are the files on the disk to be processed.
The file consists of three parts: file Lujin + file name backbone + file suffix
For example: C:\ code\ test.txt
II. File type
According to the organization of the data, data files are called text files and binary files.
ASCII files, also known as text files, are stored on disk with a byte of each character, which is used to store the corresponding ASCII code.
The data is stored in memory in binary form. If the output is not converted to the outer layer, it is a binary file.
For example, the storage form of the number 1000 is:
ASCLL code form: 00110001 00110000 00110000 00110000 occupies 5 bytes
Binary storage: 00000000 00000000 00100111 00010000 occupies 4 bytes
III. File buffer
When we learn the C language file operation, we also need to briefly understand the C language file buffer concept:
The Buffer, also known as Cache, is part of the memory space. In other words, the computer reserves a certain amount of storage space in memory to temporarily store input or output data, which is called buffer (cache).
There are output buffers and input buffers between the program data area and the disk. If you are interested, you can take a closer look.
The buffer zone is designed to coordinate the work of low-speed input and output devices and high-speed user programs, and reduce the number of read and write times of input and output devices.
IV. File pointer
In the buffer file system: the key concept is "file type pointer", referred to as * "file pointer" * *
each used file has opened up a corresponding file information area in memory, which is used to store the relevant information of the file (such as the name, status and current location of the file, etc.). This information is stored in a structure variable. The structure variable is systematically declared to be named FILE
The header file stdio.h of the vs2013 compiler used has the following file type names:
The FILE of different c compilers contains different contents, but they are more or less the same.
Whenever you open a file, the system automatically creates a variable of the FILE structure according to the condition of the file and populates the information in it.
Variables of this structure are generally maintained through a pointer to FILE.
FILE* pf;// file pointer variable
Defines pf as a pointer variable to FILE type data that enables pf to point to the file information area (which is a structure variable) of a file. The file can be accessed through the information in the information area of the file. That is, the file associated with it can be found through the file pointer variable
5. Opening and closing of files
The file should be opened before reading and writing, and should be closed after use.
When you write a program, when you open the file, you will return a pointer variable of FILE pointing to the file, which is equivalent to establishing the relationship between the pointer and the file.
The fopen function opens the file, and fclose closes the file
FILE * fopen (const char * filename, const char * mode); int fclose (FILE * stream)
There are generally two ways to open a file:
Open the file under the current road strength (relative path)
Absolute path open
/ / Open by reading / / Open the file test.txtfopen ("test.txt", "r") in the current path (relative path); / / Open fopen in absolute path ("E:\ C++file\\ file\\ test.txt", "r")
The opening method is as follows:
"r" (read-only) opens an existing text file for input
"w" (write only) opens a text file for output
"a" (append) add data to the end of the file
"rb" (read only) opens a binary file for input
"wb" (write only) opens a binary file for the output
"r +" (read and write) opens a text file for read and write
"w +" (read and write) creates a new text file for read and write
"a +" (read and write) opens a text file for read and write
"rb+" (read and write) opens a binary file for read and write
"wb+" (read and write) creates a new binary file for read and write
"ab+" (read and write) opens a binary file for read and write
Note:
Data cannot be entered into a file opened in "r" mode, and the file already exists, otherwise an error will occur.
If you open a file in "w" mode, you can only enter data into it. If the open file does not exist, create a new file with the specified name when opening it. If the specified file exists, delete the file when you open it, and then create a new file.
If the function fopen fails to open the file, fopen returns an empty pointer value NULL
When the program starts to run, the system automatically opens three standard files: standard input (stdin), standard output (stdout), and standard error output (stderr). If you want to use the input and output terminal, you don't need to open it, you can use it directly, such as fputc (stdout,'a'); output the character a to the screen.
Opening a file as a read must exist
A file opened as a write can only enter data into the file. If the open file does not exist, create a new file with the specified name when opening; if the specified file exists, delete the file when you open it, and then create a new file.
This is the end of the content of "how to open and close C language files". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.