知识屋:更实用的电脑技术知识网站
所在位置:首页 > 操作系统 > linux

linux文件操作之close

发布时间:2014-09-05 16:04:39作者:知识屋


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)  www.zhishiwu.com  
    {
         cerr << "error!" << endl;
    }
    close(fd1);
    return 0;
}
猜一下上面程序的执行结果。
在write时,fd和fd1的值是一样的,write fd时,实际就是write fd1。
为了避免自己在程序中误操作,可以在close(fd)之后,加上fd = -1;
 
 
作者 MrRightLeft
(免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)
收藏
  • 人气文章
  • 最新文章
  • 下载排行榜
  • 热门排行榜