当前位置:网站首页>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
边栏推荐
- 394. String decoding - auxiliary stack
- Oil monkey website address
- Amount input box, used for recharge and withdrawal
- Go's gin framework learning
- 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!!!)
- 极致体验,揭晓抖音背后的音视频技术
- C1 notes [task training chapter I]
- 41. The first missing positive number
- Theory and practice of laser slam in dark blue College - Chapter 2 (odometer calibration)
- 开源按键组件Multi_Button的使用,含测试工程
猜你喜欢
394. String decoding - auxiliary stack
C# 网络相关操作
470. Rand10() is implemented with rand7()
Comparison between xtask and kotlin coroutine
2022制冷与空调设备运行操作判断题及答案
干货 | 快速抽取缩略图是怎么练成的?
2022 judgment questions and answers for operation of refrigeration and air conditioning equipment
JS high frequency interview questions
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
MySQL 中的字符串函数
随机推荐
402. Remove K digits - greedy
48. 旋转图像
Some questions some questions some questions some questions
.105Location
Add animation to the picture under V-for timing
Arithmetic expression
Submit local warehouse and synchronize code cloud warehouse
Sword finger offer 03 Duplicate number in array
Eigen learning summary
C#的随机数生成
_ FindText error
Welcome to the markdown editor
2022 Shanghai safety officer C certificate operation certificate examination question bank and simulation examination
剑指 Offer 22. 链表中倒数第k个节点-快慢指针
Land cover / use data product download
On the problem of V-IF display and hiding
2022年上海市安全员C证操作证考试题库及模拟考试
ES6 face test questions (reference documents)
This point in JS
Special effects case collection: mouse planet small tail