当前位置:网站首页>【C语言】文件读写函数使用
【C语言】文件读写函数使用
2022-06-28 11:36:00 【贾璞】
综合描述按字符,按行,按块读写方式。
#include <stdio.h>
#include <string.h>
void fileCharWrite() {
FILE *file_write = fopen("./test01.txt", "w");
if (!file_write) {
//ERROR: No such file or directory
perror("ERROR");
return;
}
char ch = 'a';
for (int i = 0; i < 5; ++i) {
fputc(ch, file_write);
}
fclose(file_write);
}
void fileCharRead() {
FILE *file_read = fopen("./test01.txt", "r");
if (!file_read) {
perror("ERROR");
return;
}
//注意此时会体现出feof()的滞后性,最后会将EOF标识符赋予ch,或可在赋值后进行if判断,避免EOF
// while(!feof(file_read))
// {
// char ch = fgetc(file_read);
// printf("%c\n", ch);
// }
//以下方式亦可避免
char ch;
while ((ch = fgetc(file_read)) != EOF) {
printf("%c\n", ch);
}
fclose(file_read);
}
//按行
void fileLineReadWrite() {
//写
FILE *file_write = fopen("./test02.txt", "w");
if (!file_write) {
perror("ERROR");
return;
}
char *arr[] = {
"锄禾日当午\n",
"汗滴禾下土\n",
"谁知盘中餐\n",
"粒粒皆辛苦\n"
};
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); ++i) {
fputs(arr[i], file_write);
}
fclose(file_write);
//读
FILE *file_read = fopen("./test02.txt", "r");
if (!file_read) {
perror("ERROR");
return;
}
char temp[100] = {
0};
while (fgets(temp, 100, file_read)) {
printf("%s", temp);
}
fclose(file_read);
}
//按块
void fileBlockReadWrite() {
//写
FILE *file = fopen("./test03.txt", "w");
if (!file) {
perror("ERROR");
return;
}
char *arr[] = {
"锄禾日当午\n",
"汗滴禾下土\n",
"谁知盘中餐\n",
"粒粒皆辛苦\n"
};
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); ++i) {
fwrite(arr[i], strlen(arr[i]), 1, file);
}
fclose(file);
//读
file = fopen("./test03.txt", "r");
if (!file) {
perror("ERROR");
return;
}
char temp[100] = {
0};
while (fread(temp, strlen(arr[0]), 1, file)) {
printf("%s", temp);
}
fclose(file);
}
void test() {
//fileCharWrite();
//fileCharRead();
//fileLineReadWrite();
fileBlockReadWrite();
}
int main(int argc, char const *argv[]) {
test();
return 0;
}
边栏推荐
- AcWing 604. Area of circle (implemented in C language)
- day25 js中的预解析、递归函数、事件 2021.09.16
- What is DAPP system development and analytical understanding
- Use logrotate to automatically cut the website logs of the pagoda
- Why do many people want to change careers as programmers, while some programmers want to change careers as others?
- 6.A-B
- Is it feasible to be a programmer at the age of 26?
- 2018 joint examination of nine provinces & Merging of line segment trees
- Dongyuhui, New Oriental and Phoenix Satellite TV
- 【C语言】二叉树的实现及三种遍历
猜你喜欢

Convert black mask picture to color annotation file

Industry analysis - quick intercom, building intercom

Redis principle - List

day29 js笔记 2021.09.23

Leetcode 705. 设计哈希集合

Why do many people want to change careers as programmers, while some programmers want to change careers as others?

Database Series: is there any way to seamlessly upgrade the business tables of the database

day37 js笔记 运动函数 2021.10.11

Day39 prototype chain and page Fireworks Effect 2021.10.13

Web3安全连载(3) | 深入揭秘NFT钓鱼流程及防范技巧
随机推荐
day33 js笔记 事件(下)2021.09.28
【无标题】虚拟机vmnet0找不到且报错:没有未桥接的主机网络适配器
【北京航空航天大学】考研初试复试资料分享
Contract quantification system development (construction explanation) - contract quantification system development (source code analysis and ready-made cases)
来吧元宇宙,果然这热度一时半会儿过不去了
Day24 JS notes 2021.09.15
AcWing 608. Poor (implemented in C language)
2022 open source software security status report: over 41% of enterprises do not have enough confidence in open source security
赛尔号抽奖模拟求期望
Jetpack Compose Desktop 桌面版本的打包和发布应用
面试步骤的面试技巧
ProCAST finite element casting process simulation software
水果FL Studio/Cubase/Studio one音乐宿主软件对比
Day32 JS note event (Part 1) September 27, 2021
Unity screenshot function
网页提示此站点不安全解决方案
Day23 JS notes 2021.09.14
day39 原型鏈及頁面烟花效果 2021.10.13
Zero basic C language (I)
Day36 JS notes ecma6 syntax 2021.10.09