linux文件操作之close
linux文件操作之close
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.2cto.com
{
cerr << "error!" << endl;
}
close(fd1);
return 0;
}
猜一下上面程序的执行结果。
在write时,fd和fd1的值是一样的,write fd时,实际就是write fd1。
为了避免自己在程序中误操作,可以在close(fd)之后,加上fd = -1;
作者 MrRightLeft
评论暂时关闭