当前位置:网站首页>【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;
}
边栏推荐
- 【C语言】如何产生正态分布或高斯分布随机数
- 零基础C语言(一)
- Deployment and optimization of vsftpd service
- Ali three sides: what is the difference between using on or where in the left join associated table and the condition
- day32 js笔记 事件(上)2021.09.27
- Come on, yuanuniverse. Sure enough, the heat won't pass for a while
- Leetcode 705. 设计哈希集合
- Difference (one dimension)
- 來吧元宇宙,果然這熱度一時半會兒過不去了
- 无法重新声明块范围变量
猜你喜欢

Everyone can participate in open source! Here comes the most important developer activity in dragon lizard community

Class pattern and syntax in JS 2021.11.10

Packaging and publishing application of jetpack compose desktop version

Prefix and (2D)

【C语言】二叉树的实现及三种遍历

SEO优化的许多好处是与流量有直接关系

Tidb v6.0.0 (DMR): initial test of cache table - tidb Book rush

Graphics view framework for QT learning (to realize startup animation)

Day36 JS notes ecma6 syntax 2021.10.09

Web page tips this site is unsafe solution
随机推荐
Bisection (integer bisection and floating point bisection)
Web3 security serials (3) | in depth disclosure of NFT fishing process and prevention techniques
The default point of this in JS and how to modify it to 2021.11.09
Which programming language will attract excellent talents?
What is the main chain system?
Chapter 2 do you remember the point, line and surface (2)
day25 js中的预解析、递归函数、事件 2021.09.16
Share the easy-to-use fastadmin open source system - practical part
案例驱动 :从入门到掌握Shell编程详细指南
Apache2 configuration denies access to the directory, but can access the settings of the files inside
Industry analysis - quick intercom, building intercom
Deployment and optimization of vsftpd service
Day39 prototype chain and page Fireworks Effect 2021.10.13
Excel import / export convenience tool class
Interview skills for interview steps
Day36 JS notes ecma6 syntax 2021.10.09
Cohere, a large model company, completed the round B financing of US $125million
day33 js笔记 事件(下)2021.09.28
Day37 JS note motion function 2021.10.11
Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method