为什么 O_RDWR 不给我这段代码的读写权限?
why do O_RDWR doesn't give me write and read permission in this code?
我开始对 C
中的 file descriptors
感兴趣,我写了以下代码:
int main (void)
{
int fd1, fd2, sz;
char *buf = "hello world !!!";
char *buf2 = malloc(strlen(buf));
fd1 = open ("test.txt", (O_RDWR | O_CREAT));
printf("file fd1 created\n");
write(fd1, buf, strlen(buf));
printf("write %s, in filedescpror %d\n", buf, fd1);
sz = read(fd1, buf2, strlen(buf));
printf("read %s, with %d bytes from file descriptor %d\n", buf2, sz, fd1);
close(fd1);
fd2 = open ("testcpy.txt", (O_RDWR | O_CREAT));
write(fd2, buf2, strlen(buf));
close(fd2);
return 0;
}
通常:
- 提供了两个具有读写权限的文件,
buf
粘贴到 fd1
fd1
被读取,数据存入bf2
bf2
解析为fd2
第一个问题是我在结果中获得的权限不正确,发生的事情是 buf2
之外的内容被解析为 fd2
.
任何人都可以告诉我发生了什么,我的代码是错误的,还是正在发生的是预期的行为。
您需要在 write()
之后倒带 buff,并添加权限(open()
的第 3 个参数),这是一个基本示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
int main (void)
{
int fd1, fd2, sz;
char *buf = "hello world !!!";
char *buf2 = malloc(strlen(buf) + 1); // space for '[=10=]'
fd1 = open ("test.txt", (O_RDWR | O_CREAT), 777); // 3rd arg is the permissions
printf("file fd1 created\n");
write(fd1, buf, strlen(buf));
lseek(fd1, 0, SEEK_SET); // reposition the file pointer
printf("write %s, in filedescpror %d\n", buf, fd1);
sz = read(fd1, buf2, strlen(buf));
buf[sz] = '[=10=]';
printf("read %s, with %d bytes from file descriptor %d\n", buf2, sz, fd1);
close(fd1);
fd2 = open ("testcpy.txt", (O_RDWR | O_CREAT));
write(fd2, buf2, strlen(buf));
close(fd2);
return 0;
}
我开始对 C
中的 file descriptors
感兴趣,我写了以下代码:
int main (void)
{
int fd1, fd2, sz;
char *buf = "hello world !!!";
char *buf2 = malloc(strlen(buf));
fd1 = open ("test.txt", (O_RDWR | O_CREAT));
printf("file fd1 created\n");
write(fd1, buf, strlen(buf));
printf("write %s, in filedescpror %d\n", buf, fd1);
sz = read(fd1, buf2, strlen(buf));
printf("read %s, with %d bytes from file descriptor %d\n", buf2, sz, fd1);
close(fd1);
fd2 = open ("testcpy.txt", (O_RDWR | O_CREAT));
write(fd2, buf2, strlen(buf));
close(fd2);
return 0;
}
通常:
- 提供了两个具有读写权限的文件,
buf
粘贴到fd1
fd1
被读取,数据存入bf2
bf2
解析为fd2
第一个问题是我在结果中获得的权限不正确,发生的事情是 buf2
之外的内容被解析为 fd2
.
任何人都可以告诉我发生了什么,我的代码是错误的,还是正在发生的是预期的行为。
您需要在 write()
之后倒带 buff,并添加权限(open()
的第 3 个参数),这是一个基本示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
int main (void)
{
int fd1, fd2, sz;
char *buf = "hello world !!!";
char *buf2 = malloc(strlen(buf) + 1); // space for '[=10=]'
fd1 = open ("test.txt", (O_RDWR | O_CREAT), 777); // 3rd arg is the permissions
printf("file fd1 created\n");
write(fd1, buf, strlen(buf));
lseek(fd1, 0, SEEK_SET); // reposition the file pointer
printf("write %s, in filedescpror %d\n", buf, fd1);
sz = read(fd1, buf2, strlen(buf));
buf[sz] = '[=10=]';
printf("read %s, with %d bytes from file descriptor %d\n", buf2, sz, fd1);
close(fd1);
fd2 = open ("testcpy.txt", (O_RDWR | O_CREAT));
write(fd2, buf2, strlen(buf));
close(fd2);
return 0;
}