当前位置:网站首页>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
边栏推荐
- Chapter 5 Basic Scripting: Shell Variables
- How developers choose the right database for react native
- srec_ Use of common cat parameters
- C盘空间不够 mklink解决VScode扩展迁移到其他盘
- 乐观锁解析
- If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing
- Analysis of regression problem, modeling and prediction
- Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
- Flexible current probe selection guide
- 【网页性能优化】SPA(单页面应用)首屏加载速度慢怎么办?
猜你喜欢
随机推荐
Use of C language cjson Library
曾拿2亿融资,昔日网红书店如今全国闭店,60家店仅剩3家
MySQL 索引优化全攻略
11.2-hj86 find the maximum number of continuous bits
TypeError: Unrecognized value type: <class ‘str‘> ParserError: Unknown string format
c语言---25 扫雷游戏
[HAOI2015]树上操作
Tkinter GUI address book management system
Interview shock: why does TCP need three handshakes?
JVM基础和问题分析入门笔记
VIM basic operation commands
vim基本操作命令
论文修改回复1
Disk performance and capacity
[web page performance optimization] what about the slow loading speed of the first screen of SPA (single page application)?
C language -- 25 minesweeping game
Nc78 reverse linked list
Stm8s003f3 internal flash debugging
ZFS - 01 - basic operations of creating and expanding zpool
关爱一线防疫工作者,浩城嘉业携手高米店街道办事处共筑公益长城








