当前位置:网站首页>【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;
}
边栏推荐
- Day31 JS notes DOM 2021.09.26
- Database Series: is there any way to seamlessly upgrade the business tables of the database
- AcWing 606. Average 1 (implemented in C language)
- Unity screenshot function
- QML control type: tabbar
- NFT card chain game system development DAPP construction technical details
- Leetcode 705. 设计哈希集合
- Research on personalized product search
- Characteristics of solar wireless LED display
- Day24 JS notes 2021.09.15
猜你喜欢

Is it feasible to be a programmer at the age of 26?

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

Multi dimensional monitoring: the data base of intelligent monitoring

Chendanqi, Fang Fei, guquanquan and Li Bo won the prize, and the list of Sloan research award in 2022 was released

行业分析| 快对讲,楼宇对讲

Day30 JS notes BOM and DOM 2021.09.24

Day39 prototype chain and page fireworks effect 2021.10.13

Recommended practice sharing of Zhilian recruitment based on Nebula graph

Redis principle - List

Day32 JS note event (Part 1) September 27, 2021
随机推荐
Remote login sshd service
day30 js笔记 BOM和DOM 2021.09.24
IO stream of file and Base64
Day32 JS note event (Part 1) September 27, 2021
day33 js笔记 事件(下)2021.09.28
Everyone can participate in open source! Here comes the most important developer activity in dragon lizard community
Use logrotate to automatically cut the website logs of the pagoda
MapReduce项目案例1
2018 joint examination of nine provinces & Merging of line segment trees
Day31 JS notes DOM 2021.09.26
What is DAPP system development and analytical understanding
day25 js中的预解析、递归函数、事件 2021.09.16
Bisection (integer bisection and floating point bisection)
NFT card chain game system development DAPP construction technical details
Characteristics of solar wireless LED display
ArrayList源码解析
[no title] the virtual machine vmnet0 cannot be found and an error is reported: there is no un bridged host network adapter
Open3d manual clipping point cloud
The development and principle of the metacosmic system
Tidb v6.0.0 (DMR): initial test of cache table - tidb Book rush