当前位置:网站首页>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 :
边栏推荐
猜你喜欢

图形管线基础(番外篇)

雨伞上的水滴效果

Using C language to realize three piece chess games

About the collation of shader keyword

中国剩余定理 个人理解

SSAO by computer shader (II)

Problem solving for ACM freshmen in Jiangzhong on October 26

AQS之ReentrantLock源码解析

Leetcode brush question diary sword finger offer II 055. binary search tree iterator

AQS之countDownLatch源码分析
随机推荐
OJ 1089 春运
图形管线基础(一)
数组转链表
Source code analysis of countdownlatch of AQS
网络——传输层(详细版)
archery数据库审核平台部署
Lancher deployment practice
Graphic pipeline foundation (part outside)
Redis implementation of distributed lock and analysis of the main process of redismission distributed lock
@PostConstruct注解及用处示例
feignclient @RequestMapping参数设置及请求头简易方式设置
Leetcode brush questions diary sword finger offer II 047. Binary tree pruning
TCP/IP五层模型
SSAO by computer shader (II)
ZOJ Problem 1005 jugs
OJ 1284 counting problem
Optimization ideas from ordinary query commodities to highly concurrent query commodities
redis缓存设计与性能优化
Project compilation nosuch*** error problem
[pta-- use queues to solve the problem of monkeys choosing kings]