当前位置:网站首页>文件常用操作
文件常用操作
2022-07-25 18:26:00 【菜菜小蒙】
目录
(一)文件的打开与关闭
文件在读写之前应该先打开文件,在使用结束之后应该关闭文件。
在编写程序的时候,在打开文件的同时,都会返回一个FILE*的指针变量指向该文件,也相当于建立了指针和文件的关系。ANSIC 规定使用fopen函数来打开文件,fclose来关闭文件。
打开文件:
FILE * fopen ( const char * filename, const char * mode );关闭文件:
int fclose ( FILE * stream );例:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
//打开文件
FILE* fp = fopen("test.txt", "r"); //以读取的形式打开文件
//关闭文件
fclose(fp);
return 0;
}文件打开方式类型补充:
| 文件使用方式 | 含义 | 如果指定文件不存在 |
| "r" | 输入数据,打开一个存在的文本文件 | 出错 |
| "w" | 输出是数据,打开一个文本文件 | 建立一个新的文件 |
| "a" | 向文本文件尾添加数据 | 建立一个新的文件 |
| "rb" | 输入数据,打开一个二进制文件 | 出错 |
| "wb" | 输出数据,打开一个二进制文件 | 建立一个新的文件 |
| "ab" | 向一个二进制为文件尾添加数据 | 出错 |
| "r+" | 为了读和写,打开一个文本文件 | 出错 |
| "w+" | 为了读和写,建立一个新的文件 | 建立一个新的文件 |
| "a+" | 打开一个文件,再文件尾进行读写 | 建立一个新的文件 |
| "rb+" | 为了读和写打开一个二进制文件 | 出错 |
| "wb+" | 为了读和写,建立一个新的二进制文件 | 建立一个新的文件 |
| "ab+" | 打开一个二进制文件,为尾进行读和写 | 建立一个新的文件 |
(二)常用文件操作函数
(1)fgetc和fputc
fgetc:
int fgetc ( FILE * stream );fputc:
int fputc ( int character, FILE * stream );例:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
//输出
FILE* fout = fopen("test.txt", "w");
//判断
if (fout == NULL)
{
perror("Error opening file");
return 0;
}
fputc('A', fout);
fclose(fout);
//输入
FILE* fin = fopen("test.txt", "r");
//判断
if (fin == NULL)
{
perror("Error opening file");
return 0;
}
char c = fgetc(fin);
printf("%c", c);
fclose(fin);
return 0;
}
(2)fgets和fputs
fgets
char * fgets ( char * str, int num, FILE * stream );fputs
int fputs ( const char * str, FILE * stream );例:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
//输出
FILE* fout = fopen("test.txt", "w");
//判断
if (fout == NULL)
{
perror("Error opening file");
return 0;
}
char arr1[] = "abcdefgh";
fputs(arr1, fout);
fclose(fout);
//输入
FILE* fin = fopen("test.txt", "r");
//判断
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和fprintf
fscanf
int fscanf ( FILE * stream, const char * format, ... );fprintf
int fprintf ( FILE * stream, const char * format, ... );例:
#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;
//输出
FILE* fout = fopen("test.txt", "w");
//判断
if (fout == NULL)
{
perror("Error opening file");
return 0;
}
fprintf(fout, "%d %d %c", s.a, s.b, s.c);
fclose(fout);
//输入
FILE* fin = fopen("test.txt", "r");
//判断
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和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 );例:
#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;
//输出
FILE* fout = fopen("test.txt", "w");
//判断
if (fout == NULL)
{
perror("Error opening file");
return 0;
}
fwrite(&s, sizeof(struct S), 1, fout);
fclose(fout);
//输入
FILE* fin = fopen("test.txt", "r");
//判断
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;
}(三)文件读取结束的判定
文本文件读取是否结束,判断返回值是否为 EOF ( fgetc ),或者 NULL ( fgets )
- fgetc 判断是否为 EOF
- fgets 判断返回值是否为 NULL
- fread判断返回值是否小于实际要读的个数
边栏推荐
- win11下vscode 自动升级失败 There was an error while marking a file for deletion
- CircleIndicator组件,使指示器风格更加多样化
- 11.1-cm24 nearest common ancestor
- 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
- Basic knowledge of documents
- mysql的小数number类型select之后丢失了前面的0
- Use of join function in MATLAB
- Diagonalization, power of a
- [Huawei machine test real question] string matching
- [noi2015] package manager
猜你喜欢

Stm8s003f3 internal flash debugging

超全Mavan标签详解

这是一张机器&深度学习代码速查表

pd.melt() vs reshape2::melt()

408第二章线性表

Thales launches solutions to help SAP customers control cloud data

Kendryte K210 在freertos上的lcd屏幕的使用

Boomi won the "best CEO in diversity" and the "best company in career growth" and ranked among the top 50 in the large company category

408 Chapter 2 linear table

One week activity express | in simple terms, issue 8; Meetup Chengdu station registration in progress
随机推荐
Optimistic lock resolution
STM32F105RBT6 内部flash调试
pd.melt() vs reshape2::melt()
C语言 libcurl交叉编译
[noi2015] package manager
Boomi won the "best CEO in diversity" and the "best company in career growth" and ranked among the top 50 in the large company category
JZ71 跳台阶扩展问题
PHP memory management mechanism and garbage collection mechanism
Optimistic lock pessimistic lock applicable scenario
C language libcurl cross compilation
Sequential storage structure, chain storage structure and implementation of stack
The milestone progress has been made in the joint development of whole human GPCR antibody drugs by baicalto and liberothera
VIM basic operation commands
Related operations of binary tree
【网页性能优化】SPA(单页面应用)首屏加载速度慢怎么办?
Tensor to img & imge to tensor (tensor conversion of pytorch)
vim基本操作命令
C language -- 25 minesweeping game
Related operations of figure
11.1-cm24 nearest common ancestor