不允许txt文件的权限在POSIX C中读写

时间:2016-07-11 14:54:30

标签: c posix

到目前为止我已经完成了以下代码。它创建文件,但其属性中的权限访问不是读写。因此,当我启用它并再次运行程序并输入相同的文件名。它工作得很好。 所以问题是,如何在代码中访问要读写的文件?

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#define BLOCK_SIZE 4096

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#define BLOCK_SIZE 4096

int main()
{
   int readfile=-1,openfile=-1, size=0, i=0;
   char str[100],fname[50],new_fname[50];
   printf("Enter name of file to copy: ");
   scanf("%s",fname);
   printf("Enter name of new file:");
   scanf("%s",new_fname);

   readfile=open(fname,O_CREAT|O_APPEND|O_RDONLY);
   if(readfile == -1)
   {
      printf("Open Failed");
      return 1;
   }
   else 
   {
     size=read(readfile,str,BLOCK_SIZE);
     openfile=open(new_fname,O_CREAT|O_WRONLY|O_EXCL);
     write(openfile,str,size);
     flock(openfile,LOCK_UN);
   }

   close(openfile);
   close(readfile);
   return 0;
}

0 个答案:

没有答案