当前位置:网站首页>标准C语言16
标准C语言16
2022-08-06 08:01:45【洛cs】
三十九、文件
文件分类
文本文件:存储的是ASCII码的二进制
二进制文件:存储的是数据的补码
文件IO
#include <stdio.h>
FILE *fopen(const char *path, const char *mode);
功能:打开或创建文件
path:文件路径
mode:文件打开模式;
r 以只读权限打开文件,不存在就失败
r+ 在r的基础上增加写的权限
w 已只写权限打开文件,如果存在则清空打开,不存在则新建文件
w+ 在w的基础上增加写的权限
a 已只写权限打开文件,存在则在末尾追加,不存在则新建
a+ 在a的基础上增加写的权限
返回值:文件指针,不需要关心里面是什么数据,是一个针对已打开的凭借。打开失败返回NULL
二进制方式的读写
#include <stdio.h>
size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
功能:把内存中的数据写入到文件中
ptr:要写入内存的首地址
size:一次写入的字节数
nmemb:写入的次数
stream:文件指针,fopen的返回值,即往哪个文件写入
返回值:成功写入的次数
#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
功能:从文件读取数据到内存中
ptr:接收文件数据的首地址
nmemb:读取多少次
stream:文件指针,表示从哪个文件读取
返回值:成功读取的次数
文本方式读写
#include <stdio.h>
int fprintf(FILE *stream, const char *format, ...);
功能:以文本形式写入数据到文件中
stream:要写入的文件
format:占位符和提示信息
...:变量名列表
返回值:成功写入的字节数
int fscanf(FILE *stream, const char *format, ...);
功能:从文件中读取到变量中
stream:要读取的文件
format:占位符和提示信息
...:变量名列表
返回值:成功读取的变量个数
关闭文件
int fclose(FILE *stream);
功能:关闭一个打开的文件
返回值:成功返回0,失败返回-1
注意:如果需要立即修改文件的内容,最好先关闭
文件位置指针:
每个通过fopen打开的文件都有一个文件位置指针来记录接下来读写的位置,
以r、r+、w、w+打开的文件,位置指针在文件的开头,以a、a+打开的文件,位置指针在末尾
如果想要随意读写文件的位置,那么可以手动设置文件位置指针的位置
int fseek(FILE *stream, long offset, int whence);
功能:手动设置位置指针的位置
stream:文件指针
offset:偏移值
whence:基础位置
SEEK_SET 开头
SEEK_CUR 当前位置
SEEK_END 末尾
返回值:成功返回0(调整之后位置指针还在文件中),失败返回-1
long ftell(FILE *stream);
功能:获取当前文件位置指针的位置
返回值:在第几个字节
void rewind(FILE *stream);
功能:把文件指针位置设置到文件开头
int fgetc(FILE *stream);
功能:从文件中读取一个字符
返回值:失败、读取完毕返回EOF(-1)
char *fgets(char *s, int size, FILE *stream);
功能:读取一行字符串到s中,最多读取size-1个
返回值:首地址
int fputc(int c, FILE *stream);
功能:写入一个字符到文件中
返回值:成功返回非负整数,失败返回-1
int fputs(const char *s, FILE *stream);
功能:写入一个字符串到文件中
返回值:成功返回非负整数,失败返回-1
int remove(const char *pathname);
功能:删除文件
pathname:文件路径
返回值:成功返回0,失败返回-1
int rename(const char *oldpath, const char *newpath);
功能:重命名
返回值:成功返回0,失败返回-1
四十、命令行参数:(main函数的参数)
是为了获取./a.out 命令行参数
argc 代表了命令行参数个数(./a.out是第一个)
argv 每个参数字符串的首地址(./a.out是argv[0])
实现cp命令
边栏推荐
- CSDN official plug-in
- No, no, no, it's 2022, you don't know the principle of Jmeter, right?
- 测试用例设计方法-场景法详解
- 一文3000字解析Pytest单元测试框架【保姆级教程】
- 2022-08-05:以下go语言代码输出什么?A:65, string;B:A, string;C:65, int;D:报错。
- Jetpack WorkManager 看这一篇就够了~
- 关于np.zeros()第三个参数:c代表与c语言类似,行优先;F代表列优先的记录
- 腾讯云点播上传视频文件解决路径问题
- Parameter ‘courseId‘ not found. Available parameters are [arg1, arg0, param1, para
- LeetCode——345. 反转字符串中的元音字母
猜你喜欢
随机推荐
- yum离线安装
- 禁止运行游戏的程序开发
- QianBase 运维实用命令
- EsgynDB Troubleshooting - 网卡MTU导致跨网段访问数据库失败
- 按钮只能点击一次
- errorCode 1045, state 28000错误详解即解决方法
- UNIX environment advanced programming - the first chapter
- Advanced Programming in UNIX Environment - Chapter 2
- CPU Architecture at a Glance
- QianBase Operation and Maintenance Practical Commands
- About the third parameter of np.zeros(): c represents similar to c language, row priority; F represents column priority record
- Use the aggird component to implement sliding request paging to achieve the effect of infinite scrolling
- Test case design method - detailed explanation of scenario method
- Program development that runs the game is prohibited
- ErrorCode 1045, the state 28000 error, rounding the solution
- Can the code signing certificate solve the software being alerted by antivirus software?
- yum offline installation
- How much is a code signing certificate?
- Jetpack WorkManager is enough to read this article~
- Button can only be clicked once
- bpe Chinese tokens
- 20220803 Simulation
- Rock vocal | completes the safety operation, is not so difficult as you think
- Parameter ‘courseId’ not found. Available parameters are [arg1, arg0, param1, para
- 2022 Hailiang SC Travel Notes
- qwqのtechnology flag
- EsgynDB Troubleshooting - nic MTU lead to the failure across a network segment to access the database
- dalle2: hierarchical text-conditional image generation with clip
- Tencent Cloud VOD uploads video files to solve the path problem
- LeetCode - 1047. Remove all adjacent duplicates in a string
- 2022-08-05: What does the following go code output?A: 65, string; B: A, string; C: 65, int; D: error.
- LeetCode - 345. The reversal in the string vowels
- selenium4.0以上元素被定位
- 剑指 Offer 39. 数组中出现次数超过一半的数字
- 剑指 Offer 56 - I. 数组中数字出现的次数
- 剑指 Offer 33. 二叉搜索树的后序遍历序列
- 剑指 Offer 15. 二进制中1的个数,位运算,与运算
- 自动化测试定位不到元素?可能是 frame 在搞鬼
- 华为外包测试2年,不甘被替换,168天的学习转岗成正式员工
- 软件测试八款优秀的API安全测试工具,会用三款工作效率能提升50%