关于open函数的在不创建新文件时,打开不存在的文件的测试
来源:互联网 发布:游戏编程师工资高吗 编辑:程序博客网 时间:2024/06/02 07:00
虽然文件IO函数用过了很多次,但是发现编程总是会有疑问的地方,随着时间的渐进,我慢慢发现以前觉得懂的东西中还有很多不懂的东西和更深层次的东西等着自己的发现。
首先,我测试了open函数在不创建不存在的文件时(即不加O_CREATE),打开不存在的文件。
#include<stdio.h>#include<stdlib.h>#include<fcntl.h>#include<errno.h>int main(int argc, char *argv[]){ int fd; if((fd = open(argv[1], O_RDWR)) == -1){ printf("%d\n", errno); perror("open"); exit(1); } printf("%d\n", fd); return 0;}
在执行打开一个并不存在的文件后,例如
willing@willing:~/TEST/open$ ./open ./a2open: No such file or directory
然后在前面的代码中加入O_CREAT进行测试,
#include<stdio.h>#include<stdlib.h>#include<fcntl.h>#include<errno.h>#include<errno.h>#include<errno.h>int main(int argc, char *argv[]){ int fd; if((fd = open(argv[1], O_RDWR | O_CREAT)) == -1){ printf("%d\n", errno); perror("open"); exit(1); } printf("%d\n", fd); return 0;}
执行程序前:
willing@willing:~/TEST/open$ ls -ltotal 12-rwxr-xr-x 1 willing willing 5344 May 18 08:51 open-rw-r--r-- 1 willing willing 302 May 18 08:57 open.c
执行程序后
willing@willing:~/TEST/open$ ./open ./a3willing@willing:~/TEST/open$ ls -ltotal 12---------- 1 willing willing 0 May 18 08:58 a-rwxr-xr-x 1 willing willing 5344 May 18 08:58 open-rw-r--r-- 1 willing willing 302 May 18 08:57 open.c
可以看到a文件已经被建立了。
0 0
- 关于open函数的在不创建新文件时,打开不存在的文件的测试
- 打开文件open()函数的使用方法详解
- 关于C++中open打开文件函数的诡异的问题
- java在不存在文件夹的目录下创建文件
- 打开文件open()函数的使用方法详解--C语言函数
- 打开文件open()函数的使用方法详解--C语言函数
- 在sublime侧边栏点击某文件时如何在新标签页打开,而不顶替原来打开的文件
- 打开文件 fopen, open 在某些时候的区别 linux
- fstream读写创建可能不存在的文件
- java创建文件夹不存在的文件
- 关于配置android开发环境,创建新的Android Application Project时创建却不产生activity相关文件问题
- linux 学习笔记(三):open、creat、close 函数的使用,文件的创建、打开与关闭
- 创建文件函数的小测试
- 文件I/O-006.没有文件时 创建新的,若有则APPEND打开
- Linux C编程学习笔记(2):open、creat、close函数及文件的创建、打开与关闭
- 打开文件OPEN DATASET的各种语法
- 字符设备文件的打开操作 open()
- 字符设备文件的打开操作 open()
- 01-Remoting之序列化(BinaryFormatter,SoapFormatter,XmlSerializer)
- 查看mysql数据库连接数、并发数相…
- 关闭窗口前弹出对话框
- Mysql学习之路——day1
- WinService的安装与卸载
- 关于open函数的在不创建新文件时,打开不存在的文件的测试
- Ubuntu中配置nexus和maven
- asp.net 性能优化10大做法
- 数据库 + SQL 学习路线
- C#串口通信总结
- cocos2dx 3.0转场景特效
- Java 实现 RSA加密解密及数字签名
- css使用技巧总结
- jquery插件编写