fread和fwrite同时对一个文件读写
2016-02-17 21:29
411 查看
这几天看到了fopen的参数设置。中文的那些真的是不能帮助精确理解。在网上发现了英文的,特附上:
FILE *fopen(const char *filename, const char *mode)
fopen opens the named file, and returns a stream, or NULL if the attempt fails. Legal values for mode include:
"r"
open text file for reading
"w"
create text file for writing; discard previous contents if any
"a"
append; open or create text file for writing at end of file
"r+"
open text file for update (i.e., reading and writing)
"w+"
create text file for update, discard previous contents if any
"a+"
append; open or create text file for update, writing at end
Update mode permits reading and writing the same file; fflush or a file-positioning function must be called between a read and a write or vice versa. If the mode includes b after the initial letter, as in "rb" or "w+b", that indicates a binary file. Filenames are limited to FILENAME_MAX characters. At most FOPEN_MAX files may be open at once.
其中+号表示可读可写,即上面的“更新模式”。
对“更新模式”:可以同时读写,但是必须有清空缓冲区函数或者文件定位函数。意思是在读或写之间得调用文件定位的相关函数,比如fseek,rewind等等。
经我测试确实是这样。
只有这样,你才能在读之后接着写进去文件,或者rewind定位后重头部写进去。其他,都不行根本写不进去。具体原因我也不明白,用ftell测试后也发现对的,但是还是写不进去。只有这样可以,可能是什么内部实现吧。
毕竟文件读写函数是封装之后的,还是按它的标准来。
FILE *fopen(const char *filename, const char *mode)
fopen opens the named file, and returns a stream, or NULL if the attempt fails. Legal values for mode include:
"r"
open text file for reading
"w"
create text file for writing; discard previous contents if any
"a"
append; open or create text file for writing at end of file
"r+"
open text file for update (i.e., reading and writing)
"w+"
create text file for update, discard previous contents if any
"a+"
append; open or create text file for update, writing at end
Update mode permits reading and writing the same file; fflush or a file-positioning function must be called between a read and a write or vice versa. If the mode includes b after the initial letter, as in "rb" or "w+b", that indicates a binary file. Filenames are limited to FILENAME_MAX characters. At most FOPEN_MAX files may be open at once.
其中+号表示可读可写,即上面的“更新模式”。
对“更新模式”:可以同时读写,但是必须有清空缓冲区函数或者文件定位函数。意思是在读或写之间得调用文件定位的相关函数,比如fseek,rewind等等。
经我测试确实是这样。
int main() { FILE *fp = fopen("1.txt", "r+"); char Rbuf[5], Wbuf[5] = "4321"; fread(Rbuf, sizeof(char), 4, fp); Rbuf[4] = '\0'; fseek(fp,ftell(fp),SEEK_SET); fwrite(Wbuf, sizeof(char), 4, fp); printf("%s\n", Rbuf); fclose(fp); return 0; }
只有这样,你才能在读之后接着写进去文件,或者rewind定位后重头部写进去。其他,都不行根本写不进去。具体原因我也不明白,用ftell测试后也发现对的,但是还是写不进去。只有这样可以,可能是什么内部实现吧。
毕竟文件读写函数是封装之后的,还是按它的标准来。
相关文章推荐
- ffmpeg文档32-滤镜链图介绍
- Servlet原理
- 大部分人都会做错的经典JS闭包面试题
- Python笔记(2)——数据类型和数据结构
- 使用Visual Studio扩展插件Visual assist X给代码插入注释模板
- ffmpeg文档31-graph2dot
- 【小松教你手游开发】【面试必读(编程基础)】 八大排序算法
- lvs入门级知识点
- ffmpeg文档30-滤镜入门
- ORACLE无法登录错误代码 ORA-01033的解决方法
- 关于coursera上Learning How to Learn课程的读书笔记3
- Ubuntu下将文本文件转成PDF文件
- 版本控制:GitHub vs. Bitbucket
- ffmpeg文档29-放缩选项
- 注解定义和获取
- C语言之linux内核--BCD码转二进制与二进制转BCD码(笔试经典)
- C语言之linux内核--BCD码转二进制与二进制转BCD码(笔试经典)
- 【GDOI2003】排列的编码
- JavaBean
- C语言之linux内核--BCD码转二进制与二进制转BCD码