1.交换并返回错误
int main(int argc, const char *argv[])
{
int fp = open("err.txt",O_WRONLY | O_CREAT,0666);
if(fp==-1){
perror("err.txt");
return 1;}
dup2(fp,2);//重定向错误信息流到日志
int fp1 = open("1.txt",O_RDONLY);
if(fp1==-1){
perror("1.txt");
return 1;}
int fp2 = open("2.txt",O_WRONLY | O_CREAT | O_TRUNC,0666);
if(fp2==-1){
perror("2.txt");
return 1;}
char arr[128]={0};
while(1){
int b=read(fp1,arr,128);
if(b==0){break;}
write(fp2,arr,128);
}
close(fp);
close(fp1);
close(fp2);
return 0;
}
2.修改文件权限
int main(int argc, const char *argv[])
{
struct stat st;
stat("1.txt",&st);
mode_t mode1=st.st_mode;
int a=access("1.txt",W_OK);
if(a==0)
{
mode1 &= ~S_IWUSR;
chmod("1.txt",mode1);
system("ls -l ./1.txt");
}
else
{
mode1 |= S_IWUSR;
chmod("1.txt",mode1);
system("ls -l ./1.txt");
}
return 0;
}