当前位置:网站首页>[C language] use of file read / write function
[C language] use of file read / write function
2022-06-28 12:16:00 【Jia Pu】
Comprehensive description by character , Press the line , Block read / write mode .
#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;
}
// Note that this will reflect feof() The lag of , The final will be EOF Identifier assignment ch, Or it can be done after the assignment if Judge , avoid EOF
// while(!feof(file_read))
// {
// char ch = fgetc(file_read);
// printf("%c\n", ch);
// }
// The following methods can also be avoided
char ch;
while ((ch = fgetc(file_read)) != EOF) {
printf("%c\n", ch);
}
fclose(file_read);
}
// Press the line
void fileLineReadWrite() {
// Write
FILE *file_write = fopen("./test02.txt", "w");
if (!file_write) {
perror("ERROR");
return;
}
char *arr[] = {
" Hoe standing grain gradually pawning a midday \n",
" Sweat dripping under the grass \n",
" Who knows what's on the plate \n",
" Every grain is hard \n"
};
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); ++i) {
fputs(arr[i], file_write);
}
fclose(file_write);
// read
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);
}
// By block
void fileBlockReadWrite() {
// Write
FILE *file = fopen("./test03.txt", "w");
if (!file) {
perror("ERROR");
return;
}
char *arr[] = {
" Hoe standing grain gradually pawning a midday \n",
" Sweat dripping under the grass \n",
" Who knows what's on the plate \n",
" Every grain is hard \n"
};
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); ++i) {
fwrite(arr[i], strlen(arr[i]), 1, file);
}
fclose(file);
// read
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;
}
边栏推荐
- Pre parsing, recursive functions and events in day25 JS 2021.09.16
- Database Series: is there any way to seamlessly upgrade the business tables of the database
- Redis hash hash type string (5)
- NFT card chain game system development DAPP construction technical details
- 赛尔号抽奖模拟求期望
- The default point of this in JS and how to modify it to 2021.11.09
- Fruit FL studio/cubase/studio one music host software comparison
- AGCO AI frontier promotion (2.16)
- 【北京航空航天大学】考研初试复试资料分享
- .NET混合开发解决方案24 WebView2对比CefSharp的超强优势
猜你喜欢

Remote login sshd service

Android应用安全之JNI混淆

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system

【北京航空航天大学】考研初试复试资料分享

Day31 JS notes DOM 2021.09.26

Simple understanding of ThreadLocal

Day34 JS notes regular expression 2021.09.29

In less than an hour, apple destroyed 15 startups

Software test interview classic + 1000 high-frequency real questions, and the hit rate of big companies is 80%

水果FL Studio/Cubase/Studio one音乐宿主软件对比
随机推荐
[Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
Redis 原理 - List
已知两个点和中间一个比例的点,求该点坐标
JS foundation 8
Swin, three degrees! Eth open source VRT: a transformer that refreshes multi domain indicators of video restoration
Web3安全连载(3) | 深入揭秘NFT钓鱼流程及防范技巧
fatal: unsafe repository (‘/home/anji/gopath/src/gateway‘ is owned by someone else)
Url追加参数方法,考虑#、?、$的情况
AcWing 604. Area of circle (implemented in C language)
Map排序工具类
KDD 2022 | graph neural network generalization framework under the paradigm of "pre training, prompting and fine tuning"
How to deploy the software testing environment?
Daily practice of C language - day 4: find the sum of all even numbers within 100
Prepare for Jin San Yin Si I. testers without experience in automated testing projects should look at it quickly
ProCAST finite element casting process simulation software
Custom title bar view
Research on personalized product search
Connectionreseterror: [winerror 10054] the remote host forced an existing connection to be closed
MapReduce项目案例3——温度统计
Is there a threshold for opening futures accounts? How to open futures accounts safely on the Internet