当前位置:网站首页>Using files to save data (C language)
Using files to save data (C language)
2022-04-23 17:55:00 【Chshyz】
Save data to a local file
Environmental Science :CentOS7
Common file operation functions and function tables are attached
file extension
.doc(Word file )、.txt( text file )、.dat( Data files )、.c(C Language source file )、.cpp(C++ Source program files )、.for(FORTRAN Language source file )、.pas(Pascal Language source file )、.obj( Target file )、.exe( Executable file )、.ppt( Electronic document slide )、.bmp( Graphic files )、.jpg( image file ).
file type
- Program files ( Executable )
- Data files (ASCII Files and binaries )
①ASCII file : text file , Each byte stores one character ASCII Code ( Character 、 Numerical type ).
② Binary : Output the data in the memory as it is to the disk for storage ( Numerical type ).
Example : Input 10000
ASCII Output to disk as (5 byte )
Output to disk in binary form (2 byte )
The file pointer
Each used file opens up a corresponding file information area in memory to store file related information . There is a structure variable for this information (FILE) in .
example :FILE * fp
Open and close files
- Open data file (fopen)
fopen ( file name , Use file mode );
example :
FILE * fp; # Define a file that points to a variable fp
fp = fopen (“a”, “r”); # take fopen The return value of the function is assigned to fp - Close file (fclose)
fclose ( The file pointer )
example :fclose (fp);
PS:
| Use | Meaning |
|---|---|
| r | read-only (ASCII file ) |
| w | Just write (ASCII file ) |
| a | Additional ( towards ASCII Add data at the end of the file ) |
| rb | read-only ( Binary ) |
| wb | Just write ( Binary ) |
| ab | Additional ( Add data to the end of the binary file ) |
| r+ | Reading and writing ( Open one ASCII File read and write ) |
| w+ | Reading and writing ( Build a new one ASCII file ) |
| a+ | Reading and writing (ASCII file ) |
| rb+ | Reading and writing ( Binary ) |
| wb+ | Reading and writing ( Binary ) |
| ab+ | Reading and writing ( Binary ) |
Example 1: The input is sent to disk
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE * fp;
char ch, filename[10];
printf (" Please enter filename :");
scanf ("%s", filename);
if ((fp = fopen (filename, "w")) == NULL) {
printf (" Unable to open \n");
exit (0);
}
ch = getchar();
printf (" Please enter the content , With # End of no. :");
ch = getchar();
while (ch != '#') {
fputc (ch, fp);
putchar (ch);
ch = getchar ();
}
fclose (fp);
putchar (10);
return 0;
}
[[email protected] c]# gcc -o fputc fputc.c
[[email protected] c]# ./fputc
Please enter filename :one
Please enter the content , With # End of no. :happy every days!#
happy every days!
[[email protected] c]# cat one
happy every days!

版权声明
本文为[Chshyz]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230547413873.html
边栏推荐
- EasymodbusTCP之clientexample解析
- 2022年广东省安全员A证第三批(主要负责人)特种作业证考试题库及在线模拟考试
- 2022 Jiangxi Photovoltaic Exhibition, China Distributed Photovoltaic Exhibition, Nanchang Solar Energy Utilization Exhibition
- 2022江西储能技术展会,中国电池展,动力电池展,燃料电池展
- 20222 return to the workplace
- 2022江西光伏展,中国分布式光伏展会,南昌太阳能利用展
- Future usage details
- 41. The first missing positive number
- C#的随机数生成
- Arithmetic expression
猜你喜欢

Gets the time range of the current week

102. Sequence traversal of binary tree

Classification of cifar100 data set based on convolutional neural network

48. 旋转图像

C#的随机数生成

Go's gin framework learning

.105Location

Laser slam theory and practice of dark blue College Chapter 3 laser radar distortion removal exercise
![C1 notes [task training chapter I]](/img/2b/94a700da6858a96faf408d167e75bb.png)
C1 notes [task training chapter I]

EasymodbusTCP之clientexample解析
随机推荐
JS high frequency interview questions
440. The k-th small number of dictionary order (difficult) - dictionary tree - number node - byte skipping high-frequency question
Applet learning notes (I)
云原生虚拟化:基于 Kubevirt 构建边缘计算实例
92. Reverse linked list II byte skipping high frequency question
The JS timestamp of wechat applet is converted to / 1000 seconds. After six hours and one day, this Friday option calculates the time
MySQL advanced index [classification, performance analysis, use, design principles]
Implementation of image recognition code based on VGg convolutional neural network
C1小笔记【任务训练篇二】
Compilation principle first set follow set select set prediction analysis table to judge whether the symbol string conforms to the grammar definition (with source code!!!)
Anchor location - how to set the distance between the anchor and the top of the page. The anchor is located and offset from the top
Encapsulate a timestamp to date method on string prototype
48. 旋转图像
2022年流动式起重机司机国家题库模拟考试平台操作
Ring back to origin problem - byte jumping high frequency problem
Vite configure proxy proxy to solve cross domain
Oil monkey website address
Nat Commun|在生物科学领域应用深度学习的当前进展和开放挑战
ES6 new method
Go file operation