当前位置:网站首页>文件常用操作
文件常用操作
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判断返回值是否小于实际要读的个数
边栏推荐
- Linux启动mysql报错
- BL602 开发环境搭建
- 国际权威认可!OceanBase入选Forrester Translytical数据平台报告
- 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
- MySQL index optimization introduction
- Chapter 5 Basic Scripting: Shell Variables
- 超全Mavan标签详解
- Design practice of Netease strictly selecting inventory center
- 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
- Basic knowledge of documents
猜你喜欢

tkinter GUI版通信录管理系统

想要做好软件测试,可以先了解AST、SCA和渗透测试

nodejs 简单例子程序之express

Oracle uses impdp import to report an error: ora-39001: invalid parameter value ora-39000: dump file description error ora-39088: file name cannot contain path description

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

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

Detailed explanation of super full mavan label

SQL那些事

Keil5 “Loading PDSC Debug Description Failed for STMicroelectronics STM32Hxxxxxxx”解决办法

408第二章线性表
随机推荐
OV7725 yuv 640* [email protected] Profile
How to add EXE file to boot
[QNX Hypervisor 2.2用户手册]9.4 dryrun
Talking about Devops monitoring, how does the team choose monitoring tools?
BL602 开发环境搭建
Leetcode 101. symmetric binary tree & 100. same tree & 572. Subtree of another tree
php内存管理机制与垃圾回收机制
【网页性能优化】SPA(单页面应用)首屏加载速度慢怎么办?
[QNX Hypervisor 2.2用户手册]9.5 dump
使用sqldeveloper连接mysql
MySQL index optimization introduction
Is it true that CITIC Securities' low commission account opening is free of 5? Is it safe
C语言 libcurl交叉编译
Combined with GHS multi, use Reza E1 simulator to realize the simulation and debugging of Reza rh850 single chip microcomputer
Chapter 5 Basic Scripting: Shell Variables
How to create an effective help document?
一次备库的坏块的修复过程
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
win11下vscode 自动升级失败 There was an error while marking a file for deletion
STM8S003F3 内部flash调试