为什么开()创建我的错权限的文件吗? [英] Why does open() create my file with the wrong permissions?

查看:151
本文介绍了为什么开()创建我的错权限的文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文件中读取一些文本,并使用其写入到另一个的open()阅读()的write()

这是我的的open()的文件到写来(我想创建一个新的文件,并写入到它):

  FOUT =打开(测试1,O_RDWR | O_CREAT | O_SYNC);

这是设置文件权限的东西我不明白的。这是输出的ls -l <​​/ code>:

  ---------牛逼1切塔尼亚切塔尼亚0 2010-02-11 09:38测试-1

即使读取权限被锁定。我试图寻找这一点,但无法找到任何东西。
奇怪的是,的write()还是成功地将数据写入文件中。

另外,如果我做了搭配chmod 777测试-1',事情开始正常工作了。

可能有人请让我知道,我在我的公开征集回事?

谢谢!

有关您的参考,我已经粘贴了以下完整的程序:

 的#include&LT;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&fcntl.h GT;诠释主(){    字符缓冲区[512],CH;    INT FIN,FOUT,我;
    ssiz​​e_t供字节;
    FILE * FP = NULL;    //打开一个文件
    FIN =打开(测试,O_RDONLY);
    如果(FIN == -1){
        的printf(\\ nfailed打开文件。);
        返回1;
    }    //从文件中读取
    字节=读(FIN,缓冲区,缓冲区尺寸);
    //然后将其关闭
    关闭(FIN);    的printf(\\ nSuccessfully读取%d字节\\ n,字节);    //创建一个新的文件
    FOUT =打开(测试1,O_RDWR | O_CREAT | O_SYNC);    的printf(\\ n这些都是测试-1 \\ n中的权限);
    fflush(标准输出);
    系统(LS -l测试-1);    //写入并关闭它。
    写(FOUT,缓冲区,字节);
    关闭(FOUT);
    //写入以某种方式锁定,即使读取权限的文件。更改。
    系统(搭配chmod 777测试-1);    FP = FOPEN(测试1,R);
    如果(FP == NULL){
        的printf(\\ nCan't开放测试-1);
        返回1;
    }    而(1)
    {
        CH =龟etc(FP);
        如果(CH == EOF)
            打破;
        的printf(\\ N%C,CH);
    }    FCLOSE(FP);    返回0;
}


解决方案

开()接受第三个参数也就是组权限,即

 打开(文件名,O_RDWR | O_CREAT,0666)

0666是一个八进制数,即6的对应的每一个到三个权限位

6 = RW

7 = RWX

这是一个典型的陷阱。编译器允许您离开准许离开的说法,因为当你打开一个现有的文件权限位没有意义。但是当你创建一个文件时,你忘记的说法,你会得到一个随机的设置权限,例如中在您的案件0000(---)。

I am trying to read some text from a file and write it to another using open(), read() and write().

This is my open() for the file-to-write-to (I want to create a new file and write into it):

fOut = open ("test-1", O_RDWR | O_CREAT | O_SYNC);

This is setting file-permissions to something I don't understand at all. This is the output of ls -l:

---------T 1 chaitanya chaitanya 0 2010-02-11 09:38 test-1

Even the read permission is locked. I tried searching for this, but could not find ANYTHING. Strangely, write() still successfully writes data to the file.

Also, if I do a 'chmod 777 test-1', things start working properly again.

Could someone please let me know where I am going wrong in my open call?

Thanks!

For your reference, I have pasted the complete program below:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main () {

    char buffer[512], ch;

    int fIn, fOut, i;
    ssize_t bytes;
    FILE *fp = NULL;

    //open a file
    fIn = open ("test", O_RDONLY);
    if (fIn == -1) {
        printf("\nfailed to open file.");
        return 1;
    }

    //read from file
    bytes =  read (fIn, buffer, sizeof(buffer));
    //and close it
    close (fIn);

    printf("\nSuccessfully read %d bytes.\n", bytes);

    //Create a new file
    fOut = open ("test-1", O_RDWR | O_CREAT | O_SYNC);

    printf("\nThese are the permissions for test-1\n");
    fflush(stdout);
    system("ls -l test-1");

    //write to it and close it.
    write (fOut, buffer, bytes);
    close (fOut);


    //write is somehow locking even the read permission to the file. Change it.
    system("chmod 777 test-1");

    fp = fopen ("test-1", "r");
    if (fp == NULL) {
        printf("\nCan't open test-1");
        return 1;
    }

    while (1)
    {
        ch = fgetc(fp);
        if (ch == EOF)
            break;
        printf("\n%c", ch);
    }

    fclose (fp);

    return 0;
}

解决方案

open() takes a third argument which is the set of permissions, i.e.

open(filename, O_RDWR|O_CREAT, 0666)

0666 is an octal number, i.e. every one of the 6's corresponds to three permission bits

6 = rw

7 = rwx

It's a typical pitfall. The compiler allows you to leave the permission argument away, because when you open an existing file the permission bits don't make sense. But when you forget the argument when you create a file, you get a random set of permissions, e.g. 0000 in your case (---).

这篇关于为什么开()创建我的错权限的文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆