100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Linux. C open创建文件 c-为什么open()用错误的权限创建我的文件?

Linux. C open创建文件 c-为什么open()用错误的权限创建我的文件?

时间:2023-08-15 19:36:18

相关推荐

Linux. C open创建文件 c-为什么open()用错误的权限创建我的文件?

我正在尝试从文件中读取一些文本,然后使用write()、read()和write()将其写入另一个文件。

这是我的write(),用于要写入的文件(我想创建一个新文件并将其写入):

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

这将文件权限设置为我完全不了解的内容。 这是write()的输出:

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

甚至读取权限也被锁定。 我尝试搜索此内容,但找不到任何内容。奇怪的是,write()仍然成功地将数据写入文件。

另外,如果我执行“ chmod 777 test-1”,则事情将再次正常运行。

有人可以让我知道我的公开通话中哪里出问题了吗?

谢谢!

供您参考,我在下面粘贴了完整的程序:

#include

#include

#include

#include

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;

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。