当前位置:网站首页>Common file operations
Common file operations
2022-07-25 18:34:00 【Caicaixiaomeng】
Catalog
( One ) Opening and closing of files
( Two ) Common file operation functions
( 3、 ... and ) Determination of the end of file reading
( One ) Opening and closing of files
The file should be opened before reading and writing , The file should be closed after use .
In programming , While opening the file , Will return to one FILE* A pointer variable to the file , It is also equivalent to establishing the relationship between pointer and file .ANSIC To prescribe the use of fopen Function to open a file ,fclose To close the file .
Open file :
FILE * fopen ( const char * filename, const char * mode );Close file :
int fclose ( FILE * stream );example :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
// Open file
FILE* fp = fopen("test.txt", "r"); // Open the file as read
// Close file
fclose(fp);
return 0;
}File opening mode type supplement :
| How files are used | meaning | If the specified file does not exist |
| "r" | input data , Open an existing text file | error |
| "w" | Output is data , Open a text file | Create a new file |
| "a" | Add data to the end of the text file | Create a new file |
| "rb" | input data , Open a binary file | error |
| "wb" | Output data , Open a binary file | Create a new file |
| "ab" | Add data to a binary to the end of the file | error |
| "r+" | For reading and writing , Open a text file | error |
| "w+" | For reading and writing , Create a new file | Create a new file |
| "a+" | Open a file , Then read and write at the end of the file | Create a new file |
| "rb+" | Open a binary file for reading and writing | error |
| "wb+" | For reading and writing , Create a new binary | Create a new file |
| "ab+" | Open a binary file , Read and write for tail | Create a new file |
( Two ) Common file operation functions
(1)fgetc and fputc
fgetc:
int fgetc ( FILE * stream );fputc:
int fputc ( int character, FILE * stream );example :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
// Output
FILE* fout = fopen("test.txt", "w");
// Judge
if (fout == NULL)
{
perror("Error opening file");
return 0;
}
fputc('A', fout);
fclose(fout);
// Input
FILE* fin = fopen("test.txt", "r");
// Judge
if (fin == NULL)
{
perror("Error opening file");
return 0;
}
char c = fgetc(fin);
printf("%c", c);
fclose(fin);
return 0;
}
(2)fgets and fputs
fgets
char * fgets ( char * str, int num, FILE * stream );fputs
int fputs ( const char * str, FILE * stream );example :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
// Output
FILE* fout = fopen("test.txt", "w");
// Judge
if (fout == NULL)
{
perror("Error opening file");
return 0;
}
char arr1[] = "abcdefgh";
fputs(arr1, fout);
fclose(fout);
// Input
FILE* fin = fopen("test.txt", "r");
// Judge
if (fin == NULL)
{
perror("Error opening file");
return 0;
}
char arr2[10];
fgets(arr2, 5, fin);
printf("%s", arr2);
fclose(fin);
return 0;
}(3)fscanf and fprintf
fscanf
int fscanf ( FILE * stream, const char * format, ... );fprintf
int fprintf ( FILE * stream, const char * format, ... );example :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct S
{
int a;
int b;
char c;
};
int main()
{
struct S s = { 1,15,'T' };
struct S x;
// Output
FILE* fout = fopen("test.txt", "w");
// Judge
if (fout == NULL)
{
perror("Error opening file");
return 0;
}
fprintf(fout, "%d %d %c", s.a, s.b, s.c);
fclose(fout);
// Input
FILE* fin = fopen("test.txt", "r");
// Judge
if (fin == NULL)
{
perror("Error opening file");
return 0;
}
fscanf(fin, "%d %d %c", &x.a, &x.b, &x.c);
printf("%d %d %c", x.a, x.b, x.c);
fclose(fin);
return 0;
}(4)fread and fwrite
fread:
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );fwrite:
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );example :
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct S
{
int a;
int b;
char c;
};
int main()
{
struct S s = { 2,16,'M' };
struct S x;
// Output
FILE* fout = fopen("test.txt", "w");
// Judge
if (fout == NULL)
{
perror("Error opening file");
return 0;
}
fwrite(&s, sizeof(struct S), 1, fout);
fclose(fout);
// Input
FILE* fin = fopen("test.txt", "r");
// Judge
if (fin == NULL)
{
perror("Error opening file");
return 0;
}
fread(&x, sizeof(struct S), 1, fin);
printf("%d %d %c", x.a, x.b, x.c);
fclose(fin);
return 0;
}( 3、 ... and ) Determination of the end of file reading
Whether the reading of text file is finished , Determine whether the return value is EOF ( fgetc ), perhaps NULL ( fgets )
- fgetc Judge whether it is EOF
- fgets Determine whether the return value is NULL
- fread Judge whether the return value is less than the actual number to be read
边栏推荐
- Remember those two sentences
- JZ32 从上往下打印二叉树
- 结合GHS MULTI使用瑞萨E1仿真器实现对瑞萨RH850单片机的仿真调试
- Chapter 5 Basic Scripting: Shell Variables
- Detailed introduction and application of GaN (comprehensive and complete)
- NC15 求二叉树的层序遍历
- R language uses GT package and gtextras package to display tabular data beautifully: GT_ bar_ Plot function and GT_ plt_ bar_ PCT function visual percentage bar graph, percentage bar graph of original
- ServletConfig class and ServletContext class
- RTC 性能自动化工具在内存优化场景下的实践
- 408 Chapter 2 linear table
猜你喜欢

解决You can change this value on the server by setting the ‘max_allowed_packet‘ variable报错

VC/PE正跑向青岛

想要做好软件测试,可以先了解AST、SCA和渗透测试

Detailed explanation of super full mavan label

JVM基础和问题分析入门笔记

网易严选库存中心设计实践

n-queens problem

基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现

This is a quick look-up table of machine & deep learning code

Detailed introduction and application of GaN (comprehensive and complete)
随机推荐
Chapter 5 Basic Scripting: Shell Variables
Partial correlation calculation of R language and partial correlations calculation using pcor function of GGM package
JZ71 跳台阶扩展问题
C盘空间不够 mklink解决VScode扩展迁移到其他盘
How to create an effective help document?
软件测试——常用的测试工具
Trust multithread security count
1---电子实物认知
Use of join function in MATLAB
Detailed explanation of super full mavan label
11.2-HJ86 求最大连续bit数
如何将exe文件添加到开机启动
11.1-cm24 nearest common ancestor
ZFS - 01 - basic operations of creating and expanding zpool
C language -- 25 minesweeping game
If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing
Detailed introduction and application of GaN (comprehensive and complete)
Jz71 jump step expansion problem
Related operations of binary tree
CircleIndicator组件,使指示器风格更加多样化