当前位置:网站首页>Common file operations
Common file operations
2022-07-25 18:34:00 【Caicaixiaomeng】
Catalog
( One ) Opening and closing of files
( Two ) Common file operation functions
( 3、 ... and ) Determination of the end of file reading
( One ) Opening and closing of files
The file should be opened before reading and writing , The file should be closed after use .
In programming , While opening the file , Will return to one FILE* A pointer variable to the file , It is also equivalent to establishing the relationship between pointer and file .ANSIC To prescribe the use of fopen Function to open a file ,fclose To close the file .
Open file :
FILE * fopen ( const char * filename, const char * mode );Close file :
int fclose ( FILE * stream );example :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
// Open file
FILE* fp = fopen("test.txt", "r"); // Open the file as read
// Close file
fclose(fp);
return 0;
}File opening mode type supplement :
| How files are used | meaning | If the specified file does not exist |
| "r" | input data , Open an existing text file | error |
| "w" | Output is data , Open a text file | Create a new file |
| "a" | Add data to the end of the text file | Create a new file |
| "rb" | input data , Open a binary file | error |
| "wb" | Output data , Open a binary file | Create a new file |
| "ab" | Add data to a binary to the end of the file | error |
| "r+" | For reading and writing , Open a text file | error |
| "w+" | For reading and writing , Create a new file | Create a new file |
| "a+" | Open a file , Then read and write at the end of the file | Create a new file |
| "rb+" | Open a binary file for reading and writing | error |
| "wb+" | For reading and writing , Create a new binary | Create a new file |
| "ab+" | Open a binary file , Read and write for tail | Create a new file |
( Two ) Common file operation functions
(1)fgetc and fputc
fgetc:
int fgetc ( FILE * stream );fputc:
int fputc ( int character, FILE * stream );example :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
// Output
FILE* fout = fopen("test.txt", "w");
// Judge
if (fout == NULL)
{
perror("Error opening file");
return 0;
}
fputc('A', fout);
fclose(fout);
// Input
FILE* fin = fopen("test.txt", "r");
// Judge
if (fin == NULL)
{
perror("Error opening file");
return 0;
}
char c = fgetc(fin);
printf("%c", c);
fclose(fin);
return 0;
}
(2)fgets and fputs
fgets
char * fgets ( char * str, int num, FILE * stream );fputs
int fputs ( const char * str, FILE * stream );example :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
// Output
FILE* fout = fopen("test.txt", "w");
// Judge
if (fout == NULL)
{
perror("Error opening file");
return 0;
}
char arr1[] = "abcdefgh";
fputs(arr1, fout);
fclose(fout);
// Input
FILE* fin = fopen("test.txt", "r");
// Judge
if (fin == NULL)
{
perror("Error opening file");
return 0;
}
char arr2[10];
fgets(arr2, 5, fin);
printf("%s", arr2);
fclose(fin);
return 0;
}(3)fscanf and fprintf
fscanf
int fscanf ( FILE * stream, const char * format, ... );fprintf
int fprintf ( FILE * stream, const char * format, ... );example :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct S
{
int a;
int b;
char c;
};
int main()
{
struct S s = { 1,15,'T' };
struct S x;
// Output
FILE* fout = fopen("test.txt", "w");
// Judge
if (fout == NULL)
{
perror("Error opening file");
return 0;
}
fprintf(fout, "%d %d %c", s.a, s.b, s.c);
fclose(fout);
// Input
FILE* fin = fopen("test.txt", "r");
// Judge
if (fin == NULL)
{
perror("Error opening file");
return 0;
}
fscanf(fin, "%d %d %c", &x.a, &x.b, &x.c);
printf("%d %d %c", x.a, x.b, x.c);
fclose(fin);
return 0;
}(4)fread and fwrite
fread:
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );fwrite:
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );example :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct S
{
int a;
int b;
char c;
};
int main()
{
struct S s = { 2,16,'M' };
struct S x;
// Output
FILE* fout = fopen("test.txt", "w");
// Judge
if (fout == NULL)
{
perror("Error opening file");
return 0;
}
fwrite(&s, sizeof(struct S), 1, fout);
fclose(fout);
// Input
FILE* fin = fopen("test.txt", "r");
// Judge
if (fin == NULL)
{
perror("Error opening file");
return 0;
}
fread(&x, sizeof(struct S), 1, fin);
printf("%d %d %c", x.a, x.b, x.c);
fclose(fin);
return 0;
}( 3、 ... and ) Determination of the end of file reading
Whether the reading of text file is finished , Determine whether the return value is EOF ( fgetc ), perhaps NULL ( fgets )
- fgetc Judge whether it is EOF
- fgets Determine whether the return value is NULL
- fread Judge whether the return value is less than the actual number to be read
边栏推荐
- Trust multithread security count
- 结合GHS MULTI使用瑞萨E1仿真器实现对瑞萨RH850单片机的仿真调试
- What is 3DE experience platform
- Use of LCD screen of kendryte k210 on FreeRTOS
- 可视化模型网络连接
- 工程师必看的示波器探头安全使用说明书
- 对角化、A的幂
- Safe operation instructions for oscilloscope probe that must be read by engineers
- What is the difference between GB and gib disk space units?
- win11下vscode 自动升级失败 There was an error while marking a file for deletion
猜你喜欢
随机推荐
R language uses GT package and gtextras package to display tabular data beautifully: GT_ bar_ Plot function and GT_ plt_ bar_ PCT function visual percentage bar graph, percentage bar graph of original
R language ggplot2 visual line, custom configuration title text related content color and legend color match (match colors of groups)
JZ32 从上往下打印二叉树
Qtimgui 编译
3DE reply
Cve-2022-33891 Apache spark shell command injection vulnerability recurrence
If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing
Diagonalization, power of a
市值300亿,欧洲十年来最大IPO再冲纽交所
Thales launches solutions to help SAP customers control cloud data
工程师必看的示波器探头安全使用说明书
11.2-HJ86 求最大连续bit数
c语言---25 扫雷游戏
NC68 跳台阶
[QNX hypervisor 2.2 user manual]9.4 dryrun
Leetcode 101. symmetric binary tree & 100. same tree & 572. Subtree of another tree
大厂云业务调整,新一轮战争转向
MySQL 索引优化全攻略
浅析回归问题、建模、预测
rust多线程安全计数









