当前位置:网站首页>[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;
}
边栏推荐
- Privilege management of vivo mobile phone
- AGCO AI frontier promotion (2.16)
- Prepare for Jin San Yin Si I. testers without experience in automated testing projects should look at it quickly
- Prefix and (one dimension)
- .NET混合开发解决方案24 WebView2对比CefSharp的超强优势
- Zero basic C language (I)
- MapReduce project case 1
- AsyncTask经验小结
- Usage and principle of precomputedtextcompat
- Source code analysis of ArrayList
猜你喜欢

Custom title bar view

What is data compliance? How to achieve data compliance?

【C语言】关于scanf()与scanf_s()的一些问题

智联招聘基于 Nebula Graph 的推荐实践分享

【vi/vim】基本使用及命令汇总

Oracle date format exception: invalid number

KDD 2022 | graph neural network generalization framework under the paradigm of "pre training, prompting and fine tuning"

Array method in JS 2021.09.18

Deep learning has a new pit! The University of Sydney proposed a new cross modal task, using text to guide image matting

Using soapUI to obtain freemaker's FTL file template
随机推荐
fatal: unsafe repository (‘/home/anji/gopath/src/gateway‘ is owned by someone else)
面试步骤的面试技巧
Interview skills for interview steps
6.A-B
水果FL Studio/Cubase/Studio one音乐宿主软件对比
Sha256 encryption tool class
Simple understanding of ThreadLocal
Remoteviews layout and type restriction source code analysis
开源项目维权成功案例: spug 开源运维平台成功维权
【vi/vim】基本使用及命令汇总
Unity加载设置:Application.backgroundLoadingPriority
SEO优化的许多好处是与流量有直接关系
Url追加参数方法,考虑#、?、$的情况
Simulation of the Saier lottery to seek expectation
Is there a threshold for opening futures accounts? How to open futures accounts safely on the Internet
纯纯大怨种!那些年被劝退的考研专业
【C语言】如何产生正态分布或高斯分布随机数
【Unity编辑器扩展实践】、查找所有引用该图片的预制体
Day34 JS notes regular expression 2021.09.29
AsyncTask经验小结