当前位置:网站首页>[advanced C language] file operation (I)
[advanced C language] file operation (I)
2022-06-30 00:10:00 【Haozai lives today】
List of articles
1、 Program files
1、 Source file (.c For the suffix )
2、 Target file (Windows In the environment, the suffix is .obj)
3、 Executable program (Windows In the environment, the suffix is .exe)
2、 Data files
1、 One of the most common types of file systems used on computers . Essentially , It can be any file that stores a piece of data .
2、 You can use program files to read , Or write the data in the data file
3、 The file pointer
1、 When using a data file to read or write , The system will create a file information automatically FILE Structural variable , And fill in the information , Users don't have to care about details
2、 It's usually through a FILE* Pointer variables to maintain FILE This structural variable
3、 Definition pf It's a point FILE Pointer variable of type data , It can be pf Point to the file information area of a file ( It's a structural variable ). The file can be accessed through the information in the file information area . in other words , Through the file pointer variable, you can find the file associated with it
4、fopen Function to open a file ,fclose Function to close a file
#include <errno.h>
#include <stdio.h>
int main()
{
FILE* pf = fopen("text.txt","w");
// Open file
if (pf == NULL)
{
//printf(" fail to open file \n");
printf("%s\n",strerror(errno));
return 0;
}
// Writing documents
// Close file
fclose(pf);
pf = NULL;
//FILE* pf = fopen("C:\\2021code\\text.txt","w");
// If you want to generate files in a specific path , You need to add a specific path ,
// Use two \\ To eliminate the effect of transferred characters
return 0;
}
5、 How to open a folder
“r”( read-only ): For input ( Input the data in the file into the program ), Open an existing text file ( If the specified file does not exist, open the error , Returns a null pointer )
“w”( Just write ): In order to output ( Write data to a file ), Open a text file ( If the specified file does not exist, create a new file )
“a”( Additional ): Add data to the end of the text file ( If the specified file does not exist, create a new file )
4、 Sequential reading and writing of files
stdout( Standard output stream ),stdin( Standard input stream ),stderr ( Standard error flow )
1、 Write a character into the file
#include <stdio.h>
#include <errno.h>
int main()
{
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
// Writing documents
//fputc('a', pf);
//fputc('b', pf);
//fputc('c', pf);
char ch = 0;
for (ch = 'a'; ch <= 'z'; ch++)
{
fputc(ch, pf);
}
fclose(pf);
pf = NULL;
return 0;
}
2、 Read a character in the file
#include <stdio.h>
#include <errno.h>
int main()
{
FILE* pf = fopen("data.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
// Reading documents
int ch = 0;
while ((ch = fgetc(pf)) != EOF)
{
printf("%c ", ch);
}
fclose(pf);
pf = NULL;
return 0;
}
3、 Write a line
#include <stdio.h>
#include <errno.h>
int main()
{
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
//fputs("hello world\n", pf);
//fputs("hehe\n", pf);
fputs("hello world\n", stdout);//fputs The function is to write a line
//stdout Is the standard output stream , It can be printed directly on the screen
fclose(pf);
pf = NULL;
return 0;
}
4、 Read a line
#include <stdio.h>
#include <errno.h>
int main()
{
FILE* pf = fopen("data.txt", "r");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
char buf[1000] = {
0 };
// Reading documents
fgets(buf, 1000, pf);//fgets The function reads a line
printf("%s", buf);
fclose(pf);
pf = NULL;
return 0;
}
5、 Copy file code
#include <stdio.h>
#include <errno.h>
int main()
{
// Implement a code , take data.txt Copy a Generate data2.txt file
FILE* pr = fopen("data.txt", "r");
if (pr == NULL)
{
printf("open for reading: %s\n", strerror(errno));
return 0;
}
FILE* pw = fopen("data2.txt", "w");
if (pw == NULL)
{
printf("open for writting: %s\n", strerror(errno));
fclose(pr);
pr = NULL;
return 0;
}
// Copy files
int ch = 0;
while ((ch = fgetc(pr)) != EOF)
{
fputc(ch, pw);
}
fclose(pr);
pr = NULL;
fclose(pw);
pw = NULL;
return 0;
}
6、 Write formatted data
#include <stdio.h>
#include <errno.h>
struct Stu
{
char name[20];
int age;
double d;
};
int main()
{
struct Stu s = {
" Zhang San ",20,95.5 };
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
// Write formatted data
fprintf(pf, "%s %d %lf", s.name, s.age, s.d);
//fprintf(stdout, "%s %d %lf", s.name, s.age, s.d);// Write to screen
fclose(pf);
pf = NULL;
return 0;
}
7、 Read formatted data
#include <stdio.h>
#include <errno.h>
struct Stu
{
char name[20];
int age;
double d;
};
int main()
{
struct Stu s = {
" Zhang San ",20,95.5 };
FILE* pf = fopen("data.txt", "w");
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
// Read formatted data
fscanf(pf,"%s %d %lf", s.name, &(s.age), &(s.d));
printf("%s %d %lf", s.name, s.age, s.d);
fclose(pf);
pf = NULL;
return 0;
}
8、 Write in binary
#include <stdio.h>
#include <errno.h>
struct Stu
{
char name[20];
int age;
double d;
};
int main()
{
struct Stu s[2] = {
{
" Zhang San ",20,95.5 },{
"lisi",16,66.5} };
FILE* pf = fopen("data.txt", "wb");//wb It's binary
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
// Write files in binary mode
fwrite(&s,sizeof(struct Stu),2,pf);
fclose(pf);
pf = NULL;
return 0;
}
9、 Read in binary
#include <stdio.h>
#include <errno.h>
struct Stu
{
char name[20];
int age;
double d;
};
int main()
{
struct Stu s[2] = {
0 };
FILE* pf = fopen("data.txt", "rb");//rb Binary read
if (pf == NULL)
{
printf("%s\n", strerror(errno));
return 0;
}
// Write files in binary mode
fread(&s,sizeof(struct Stu),2,pf);
printf("%s %d %lf\n", s[0].name, s[0].age, s[0].d);
printf("%s %d %lf\n", s[1].name, s[1].age, s[1].d);
fclose(pf);
pf = NULL;
return 0;
}
5、 Function comparison
1、scanf( From standard input stream (stdin) Input function for formatting on )
2、printf( To standard output stream (stdout) Output function formatted on )
3、fcanf( From the standard input stream , Or specified file stream Read formatted data on )
4、fprintf( Format the data in a way Output to standard output stream , Or specify a file stream )
5、sscanf( From a string , Extract or convert formatted data )
6、sprintf( Convert a formatted data into a string )
1、sscanf and sprintf Usage of
#include <stdio.h>
#include <errno.h>
struct Stu
{
char name[20];
int age;
double d;
};
int main()
{
struct Stu s = {
" Zhang San ",20,95.5 };
struct Stu tmp = {
0 };
char buf[100] = {
0 };
sprintf(buf, "%s %d %lf", s.name, s.age, s.d);
printf("%s\n", buf);
sscanf(buf,"%s %d %lf", tmp.name, &(tmp.age), &(tmp.d));
//buf This string , Extracted in the form of structure , Namely sscanf The function of
printf("%s %d %lf\n", tmp.name, tmp.age, tmp.d);
return 0;
}
边栏推荐
- AI首席架构师9-胡晓光 《飞桨模型库与行业应用》
- Cartoon security HIDS, EDR, NDR, XDR
- Solr基础操作13
- Andorid source build/envsetup.sh 该知道的细节
- Unity about failure (delay) of destroy and ondestroy
- Leetcode (633) -- sum of squares
- Zhongang Mining: Fluorite helps the construction and development of lithium battery in fluorine industry
- Solr基础操作11
- Ingenious application of golang generics to prevent null pointer errors of variables and structural fields
- Gradle连载7-配置签名
猜你喜欢

设置安全组、域名备案、申请ssl证书
![克隆無向圖[bfs訪問每條邊而不止節點]](/img/34/2a1b737b6095293f868ec6aec0ceeb.png)
克隆無向圖[bfs訪問每條邊而不止節點]

AI chief architect 9- huxiaoguang, propeller model library and industry application

After 8 years of polishing, "dream factory of game design" released an epic update!
![Majority element ii[molar voting method for finding modes]](/img/8f/5925f97c0f5f8c50c19a9ef6d7723c.png)
Majority element ii[molar voting method for finding modes]

Unity splashimage scaling problem

Some specifications based on zfoo development project

代码分析平台 SonarQube 实战

Serialization of binary tree 297 Serialization and deserialization of binary tree 652 Find duplicate subtrees

ThinkPad VMware installation virtual machine: this host supports Intel VT-x, but Intel VT-x is disabled (problem resolution)
随机推荐
New titanium cloud service won the "2022 love analysis · panoramic report of it operation and maintenance manufacturers" cloud management platform CMP representative manufacturer
Andorid source build/envsetup. SH details to know
QT learning 01 GUI program principle analysis
JS绘制极坐标颜色渐变
[wechat applet] understand the basic composition of the applet project
shell-运算符
Cacti maximum monitoring number test
Serialization of binary tree 297 Serialization and deserialization of binary tree 652 Find duplicate subtrees
Solr基础操作7
Zhongkang holdings opens the offering: it plans to raise HK $395million net, and it is expected to be listed on July 12
modelsim的TCL脚本的define incdir命令解析
AI首席架构师9-胡晓光 《飞桨模型库与行业应用》
golang7_ TCP programming
视频ToneMapping(HDR转SDR)中的颜色空间转换问题(BT2020转BT709,YCbCr、YUV和RGB)
The role of VMware virtual machine
Halcon practical: design idea of solder joint detection
云原生爱好者周刊:炫酷的 Grafana 监控面板集合
New CorelDRAW technical suite2022 latest detailed function introduction
Code analysis platform sonarqube actual combat
Solr基础操作6