当前位置:网站首页>[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;
}
边栏推荐
- 3. seat number
- Two writing methods of JNI function
- 来吧元宇宙,果然这热度一时半会儿过不去了
- Research on personalized product search
- Map排序工具类
- Contract quantification system development (construction explanation) - contract quantification system development (source code analysis and ready-made cases)
- IO stream of file and Base64
- The development and principle of the metacosmic system
- 【C语言】如何产生正态分布或高斯分布随机数
- Day36 JS notes ecma6 syntax 2021.10.09
猜你喜欢

What is data compliance? How to achieve data compliance?

水果FL Studio/Cubase/Studio one音乐宿主软件对比

Source code analysis of ArrayList

Day34 JS notes regular expression 2021.09.29

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

In less than an hour, apple destroyed 15 startups

Day29 JS notes 2021.09.23

Day32 JS note event (Part 1) September 27, 2021

【C语言】随机数文件对其进行三种排序方法

Redis 原理 - List
随机推荐
Excel import / export convenience tool class
The development and principle of the metacosmic system
Batch will png . bmp . JPEG format pictures are converted to Jpg format picture
[no title] the virtual machine vmnet0 cannot be found and an error is reported: there is no un bridged host network adapter
【JS】斐波那契数列实现(递归与循环)
IO stream of file and Base64
Daily practice of C language - day 3: calculate the number of occurrences of sub strings of strings
Is there a threshold for opening futures accounts? How to open futures accounts safely on the Internet
JNI函数的2种书写方式
Map sorting tool class
Day32 JS note event (Part 1) September 27, 2021
. Net hybrid development solution 24 webview2's superior advantages over cefsharp
MapReduce project case 1
Use logrotate to automatically cut the website logs of the pagoda
fatal: unsafe repository (‘/home/anji/gopath/src/gateway‘ is owned by someone else)
KDD 2022 | graph neural network generalization framework under the paradigm of "pre training, prompting and fine tuning"
What method is required for word, PDF and txt files to realize full-text content retrieval?
URL append parameter method, considering #$ Situation of
cdc同步 如果数据库表的主键发生了变化,会同步成两个数据 还是会同步更新主键呢?
What is data compliance? How to achieve data compliance?