首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >fcntl不工作

fcntl不工作
EN

Stack Overflow用户
提问于 2011-02-23 20:19:37
回答 2查看 1.8K关注 0票数 1

我有一个小程序,它疲倦地更改文件访问模式后,它已被打开。

代码语言:javascript
复制
int main(int argc, char* argv[])
{
    int fd;
    char *filename = argv[1];
    char data[1];
    int curval;                         //current flag value
    int newval;                         //new flag value

fd = open(filename, O_RDONLY);

while(read(fd, data, 1)>0)
{
    write(STDOUT_FILENO, data, 1);
}

lseek(fd, 0, SEEK_SET);

if((curval = fcntl(fd, F_GETFL, 0))<0)              
{
    perror("file flag get failed");
}


printf("%d\n", curval);
    newval = curval | O_WRONLY | O_APPEND;
printf("%d\n", newval);

    if(fcntl(fd, F_SETFL, newval)<0)
{
    perror("file flag set failed");
}

if(write(fd, argv[2], strlen(argv[2]))<0)   //appending more data to the file
{
    perror("write failed");
    }

lseek(fd, 0, SEEK_SET);

while(read(fd, data, 1)>0)
{
    write(STDOUT_FILENO, data, 1);
}
close (fd);
return 0;


}


Here is the output when i run this program with a text file as input.

$ cat input
this is the inital data
$ ./a.out input newdata
this is the inital data
0
1025
write failed: Bad file descriptor
this is the inital data


Why is the write in the program failing? Also I'm not able to find where the file status flag constants are defined. I checked in usr/include/
EN

回答 2

Stack Overflow用户

发布于 2011-02-23 20:31:20

不允许您尝试执行的行为。从fcntl(2)手册页:

F\_SETFL (long) Set the file status flags to the value specified by arg. File access mode (O\_RDONLY, O\_WRONLY, O\_RDWR) and file creation flags (i.e., O\_CREAT, O\_EXCL, O\_NOCTTY, O\_TRUNC) in arg are ignored. On Linux this command can only change the O\_APPEND, O\_ASYNC, O\_DIRECT, O\_NOATIME, and O\_NONBLOCK flags.

票数 2
EN

Stack Overflow用户

发布于 2011-02-23 20:23:50

如果您阅读Linux手册页,您将看到fcntl不能更改文件访问模式(例如,从只读到读写)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5090826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档