File Operations
Concept of Files
A file is a collection of data gathered for a specific purpose. C/C++ treats each file as an ordered byte stream. Each file ends with an End-of-File (EOF) marker. To operate on a file, a program must first open it. Whenever a file is opened (remember to close it afterward), it is associated with a stream, which is essentially a sequence of bytes.
C/C++ classifies files into text files and binary files. Text files are simple text files (the main focus), while binary files include special format files or executable code files, etc.
Steps for File Operations
- Open the file, point the file pointer to the file, and decide the type of file to open.
- Perform read or write operations on the file (the main operations used in competitions; other operations are omitted here).
- Close the file after use.
freopen Function
Function Overview
This function redirects a specified input/output stream to a file in a specified mode. It is included in the header file stdio.h (cstdio). It can change the input/output environment without altering the original code, but ensure the stream is reliable when using it.
The function mainly supports three modes: read, write, and append.
Syntax
1 | |
Parameter Description
filename: The name of the file to open.mode: The mode for opening the file, indicating file access permissions.stream: The file pointer, typically using standard file streams (stdin/stdout) or the standard error output stream (stderr).- Return value: A file pointer pointing to the opened file.
File Opening Modes (Optional Reading)
r: Open file for reading only. The file must exist, and only reading data is allowed. (Commonly used)r+: Open file for reading and writing. The file must exist, and both reading and writing data are allowed.rb: Open binary file for reading only. The file must exist, and only reading data is allowed.rb+: Open binary file for reading and writing. The file must exist, and both reading and writing data are allowed.rt+: Open text file for reading and writing. Both reading and writing data are allowed.w: Open file for writing only. If the file does not exist, it will be created; otherwise, its contents will be cleared. Only writing data is allowed. (Commonly used)w+: Open file for reading and writing. If the file does not exist, it will be created; otherwise, its contents will be cleared. Both reading and writing data are allowed.wb: Open binary file for writing only. If the file does not exist, it will be created; otherwise, its contents will be cleared. Only writing data is allowed.wb+: Open binary file for reading and writing. If the file does not exist, it will be created; otherwise, its contents will be cleared. Both reading and writing data are allowed.a: Open file for writing only. If the file does not exist, it will be created. Written data will be appended to the end of the file (EOF marker is preserved).a+: Open file for reading and writing. If the file does not exist, it will be created. Written data will be appended to the end of the file (EOF marker is not preserved).at+: Open text file for reading and writing. Written data will be appended to the end of the file.ab+: Open binary file for reading and writing. Written data will be appended to the end of the file.
Usage
Read file content:
1 2 | |
Output to a file:
1 2 | |
Close standard input/output streams:
1 2 | |
Note
Functions like printf/scanf/cin/cout default to using stdin/stdout. After redirecting stdin/stdout, these functions will input/output to the redirected file.
Template
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
fopen Function (Optional Reading)
This function is largely similar to freopen. It opens the specified file and returns a pointer to the opened file.
Function Prototype
1 | |
The parameters have the same meanings as in freopen.
Available Read/Write Functions (Basic)
fread/fwritefgetc/fputcfscanf/fprintffgets/fputs
Usage
1 2 3 4 5 6 7 8 | |
C++ ifstream/ofstream File Input/Output Streams
Usage
Read file content:
1 2 | |
Output to a file:
1 2 | |
Close standard input/output streams:
1 2 | |
Template
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
References
- Informatics Olympiad One-stop Guide
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用