当前位置:网站首页>File operation in C language
File operation in C language
2022-07-28 06:48:00 【JuLiJuLi.】
Preface : We know that a program is running , Data is stored in memory , When you exit the program , The data is destroyed , The next time you run the program, you have to re-enter the data , If we want to reach the program exit , The data still exists , The data is not destroyed until we choose to delete it , How to do it ? This involves how to write data into the hard disk of the computer , In order to achieve the desired effect , It also achieves data persistence .
FILE*
First we need to understand this FILE The pointer , It is a file pointer that is used to create a FILE* To access a file , Generally, we use this file pointer to find the file associated with it .
How to open and close a file ,ANSIC To prescribe the use of fopen Function to open a file , use fclose Function to close the file .
## fopen The function prototype 
## fclose The function prototype 
When we know the function used to open the file, we also need to know how to open the file , The following figure will introduce one by one :
#include<stdio.h>
int main()
{
FILE* pf; // Create a file pointer type variable
pf = fopen("li.txt", "w"); // Open file , If it does not exist, create a new
if (pf == NULL) // Judge fopen Whether the return value of is a null pointer , Failure will return NULL The pointer
{
perror("fopen:");
return 1;
}
fclose(pf); // Close file
pf = NULL; // The pointer is set to null
return 0;
}
Here, after the program runs , At this point, a new file will be created under our path .
Sequential reading and writing of files
Sequential read / write functions and functions of files :
#include<stdio.h>
int main()
{
FILE* pf = fopen("li.txt", "w"); // Open the file as a write
if (pf == NULL)
{
perror("fopen:");
return 1;
}
char ch = 'a';
for (ch = 'a'; ch <= 'z'; ch++)
{
fputc(ch, pf); // Enter information characters into this file 'a'~'z'
}
fclose(pf); // Close file
pf = NULL;
return 0;
}
Random reading and writing of documents
1.fseek The function prototype 
This function locates the file pointer according to the position of the file pointer and the offset from the starting position .
#include<stdio.h>
int main()
{
FILE* pf = fopen("li.txt", "r"); // Because we put characters in this file 'a'~'z'
if (pf == NULL)
{
perror("fopen:");
return 1;
}
int ch = fgetc(pf); // So this is a
printf("%c\n", ch);
fseek(pf, 3, SEEK_SET); // Offset backward from the starting position 3, Point to d
ch = fgetc(pf);
printf("%c\n", ch);
ch = fgetc(pf); // Last time the pointer pointed to d, Walk backwards, this time pointing to e
printf("%c\n", ch);
fclose(pf); // Close the file after using
pf = NULL;
return 0;
}
verification :
2.ftell The function prototype 
This function is used to calculate the offset of the file pointer from the starting position at this time , And back to it .
#include<stdio.h>
int main()
{
FILE* pf = fopen("li.txt", "r"); // Because we put characters in this file 'a'~'z'
if (pf == NULL)
{
perror("fopen:");
return 1;
}
int ch = fgetc(pf); // So this is a
printf("%c\n", ch);
fseek(pf, 3, SEEK_SET); // Offset backward from the starting position 3, Point to d
ch = fgetc(pf);
printf("%c\n", ch);
ch = fgetc(pf); // Last time the pointer pointed to d, Walk backwards, this time pointing to e
printf("%c\n", ch);
long flag = ftell(pf); // Calculate the offset
printf(" The offset for the starting position is :%ld\n", flag);// The offset for the 5
fclose(pf); // Close the file after using
pf = NULL;
return 0;
}
verification :
3.rewind The function prototype 
This function returns the position of the file pointer to the starting position
#include<stdio.h>
int main()
{
FILE* pf = fopen("li.txt", "r"); // Because we put characters in this file 'a'~'z'
if (pf == NULL)
{
perror("fopen:");
return 1;
}
fseek(pf, 3, SEEK_SET); // Offset backward from the starting position 3
long flag = ftell(pf); // Calculate the offset
printf(" The offset for the starting position is :%ld\n", flag);
rewind(pf); // Use rewind Function returns the file pointer to the starting position
flag = ftell(pf); // Calculate the offset again
printf(" The offset for the starting position is :%ld\n", flag);// Here is 0
fclose(pf); // Close the file after using
pf = NULL;
return 0;
}
verification :
边栏推荐
- 项目编译NoSuch***Error问题
- [pta---- output full arrangement]
- How to simulate the implementation of strcpy library functions
- Mongodb replica set and partitioned cluster
- 水瓶效果制作
- Analysis of the semaphore source code of AQS
- [PTA----输出全排列]
- Question brushing record ---- reverse the linked list (reverse the whole linked list)
- 数组转链表
- Valgrind tool
猜你喜欢
随机推荐
TCP/IP五层模型
订单交易简析
单项链表的创建、遍历以及按要求查找结点
explain详解
【二叉树基础知识】
Graphic pipeline foundation (part outside)
ISO 3.0-server three power separation configuration
Prometheus monitoring Nacos
OJ 1505 保险丝
NiO example
[dynamic planning -- the best period for buying and selling stocks series 3]
SSAO by computer shader (III)
NFT data storage blind box + mode system development
How to store floating point data in memory
Source code analysis of countdownlatch of AQS
redis实现分布式锁思路及redission分布式锁主流程分析
Analysis of reentrantlock source code of AQS
[pta---- output full arrangement]
Leetcode brush questions diary sword finger offer II 047. Binary tree pruning
网络通信及TCP/IP协议









