#include <unistd.h>//引入头文件LINUX/UNIX下的,为函数read/write用
#include <fcntl.h>//为函数open用
int main()
{
int fdin, fdout;//定义文件描述符一个文件进的,一个出的
ssize_t nread;//定义字节大小
char buffer[1024];//定义缓冲区字节大小
fdin = open("temp1.txt", O_RDONLY);//打开当前目录下的文件temp1.txt并且以只读方式打开
fdout = open("temp2.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
//打开当前目录下的文件temp2.txt,以写方式打开,并且是没有就创建,有的话就全部覆盖掉,0644是该文件的权限 就是rwx(读/写/执行),应该明白撒
while((nread = read(fdin, buffer, 1024)) > 0)//从文件描述符里读取数据到buffer里,当读取到的字节大于0时,言外之意就是要读取完文件temp1.txt
{
if(write(fdout, buffer, nread) < nread)//把从文件temp1.txt里读到的数据一直往文件temp2.txt里写,直到写完全部temp1.txt的数据
{
close(fdin);//关闭文件描述符
close(fdout);//关闭文件描述符
}
}
close(fdin);关闭文件描述符
close(fdout);关闭文件描述符
/*基于程序的完整性,最好加上返回语句 return 0;//表示程序正常结束*/
}
不知道够不够详细呢兄弟····
温馨提示:答案为网友推荐,仅供参考