linux 文件操作之close

#include <iostream>
#include 
<fcntl.h>
using namespace std;

int main()
{
    
int fd = open("demo.txt", O_RDWR, 0);
    close(fd);
    
int fd1 = open("demo1.txt", O_RDWR, 0);
    
char *szMsg = "hello";
    
int size = write(fd, szMsg, strlen(szMsg)+1);
    
if (-1 == size)
    
{
         cerr 
<< "error!" << endl;
    }

    close(fd1);
    
return 0;
}
猜一下上面程序的执行结果。
在write时,fd和fd1的值是一样的,write fd时,实际就是write fd1。
为了避免自己在程序中误操作,可以在close(fd)之后,加上fd = -1;

posted on 2012-07-12 16:13 MrRightLeft 阅读(990) 评论(0)  编辑 收藏 引用 所属分类: C/C++


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2012年7月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

统计

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜