fopen()中w 和w+的区别
2015-09-23 12:05
302 查看
测试代码
[cpp]
view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
fp = fopen("test.txt", "w");
// fp = fopen("testt.txt", "w+");
fprintf(fp, "1This is testing for fprintf...\n");
fprintf(fp, "2This is testing for fprintf...\n");
fseek(fp, 0, SEEK_END);
long lSize = ftell(fp);
rewind(fp);
char* buf = (char*)malloc(sizeof(char) * lSize);
size_t read_size = fread(buf, 1, lSize, fp);
if (read_size != lSize) {
fputs("Reading Error\n", stderr);
exit(3);
}
printf("Reading Result:\n%s\n", buf);
fclose(fp);
free(buf);
}
如果使用"w",运行结果是
"Reading Error"
如果使用"w+",运行结果是
"Reading Result:
1This is testing for fprintf...
2This is testing for fprintf..."
所以,w的情况下下,只能write不能read,w+的情况下可以write 可以read.
官方文档这么说
[cpp]
view plaincopyprint?
![](https://code.csdn.net/assets/CODE_ico.png)
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
fp = fopen("test.txt", "w");
// fp = fopen("testt.txt", "w+");
fprintf(fp, "1This is testing for fprintf...\n");
fprintf(fp, "2This is testing for fprintf...\n");
fseek(fp, 0, SEEK_END);
long lSize = ftell(fp);
rewind(fp);
char* buf = (char*)malloc(sizeof(char) * lSize);
size_t read_size = fread(buf, 1, lSize, fp);
if (read_size != lSize) {
fputs("Reading Error\n", stderr);
exit(3);
}
printf("Reading Result:\n%s\n", buf);
fclose(fp);
free(buf);
}
如果使用"w",运行结果是
"Reading Error"
如果使用"w+",运行结果是
"Reading Result:
1This is testing for fprintf...
2This is testing for fprintf..."
所以,w的情况下下,只能write不能read,w+的情况下可以write 可以read.
官方文档这么说
"r" | read: Open file for input operations. The file must exist. |
"w" | write: Create an empty file for output operations. If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file. |
"a" | append: Open file for output at the end of a file. Output operations always write data at the end of the file, expanding it. Repositioning operations (fseek, fsetpos, rewind) are ignored. The file is created if it does not exist. |
"r+" | read/update: Open a file for update (both for input and output). The file must exist. |
"w+" | write/update: Create an empty file and open it for update (both for input and output). If a file with the same name already exists its contents are discarded and the file is treated as a new empty file. |
"a+" | append/update: Open a file for update (both for input and output) with all output operations writing data at the end of the file. Repositioning operations (fseek, fsetpos, rewind) affects the next input operations, but output operations move the position back to the end of file. The file is created if it does not exist. |
相关文章推荐
- 如何构建高可用和可伸缩的架构?
- tomcat的参数优化
- 配置Samba实现Linux和Windows系统之间的文件互访
- Linux 压缩归档总结
- Shell多线程操作及线程数控制实例
- tomcat文件详解
- 【学习笔记】系列三:Linux安装APR
- 【学习笔记】系列四:Linux安装PCRE
- Linux 定时任务 crontab
- 【学习笔记】系列二:Linux安装配置apache
- linux在shell中获取时间
- 腾讯的网站和应用都在用什么搜索?
- Linux服务器--2.DNS服务器的主从配置
- 清理linux swap内存,需要root权限
- 红帽Redhat6.4 iso下载地址
- CentOS 5.5安装gcc
- 安装apr来提高tomcat 的可伸缩性和性能
- CentOS 6.3下部署LVS(NAT)+keepalived实现高性能高可用负载均衡
- ecshop if标签,超过N条,就输出记录 elseif、库存显示方式
- linux安装rpm包时遇到Failed dependencies的解决办法