刚开始学习Linux c编程的菜鸟,本来想测试使用unlink函数创建临时文件,测试条件如下,已经存在文件 filein.txt,程序创建fileout.txt和filetemp.txt,首先拷贝filein.txt到filetemp.txt,然后再从filetemp.txt拷贝到fileout.txt,测试结果发现filetemp文件正常,但是fileout.txt为空,不知道为什么。。。代码如下
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
int main()
{
int filein, fileout, filetemp;
char buffer[128];
int nread;
filein = open("filein.txt", O_RDONLY);
filetemp = open("filetemp.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
fileout = open("fileout.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
//拷贝filein.txt至filetemp.txt
while( (nread = read(filein, buffer, sizeof(buffer) ) ) > 0)
write(filetemp, buffer, nread);
lseek(filetemp, 0, SEEK_SET);//重置filetemp.txt文件位置
//拷贝filetemp.txt至fileout.txt
while( ( nread = read( filetemp, buffer, sizeof(buffer) ) ) > 0)
write(fileout, buffer, nread);
close(filetemp);
// unlink("filetemp.txt");
return 0;
}
filetemp = open("filetemp.txt", O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
在这一句上你打开 filetemp.txt 的方式是 CREATE + WRITE,没有赋 READ 的权限。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。