简体   繁体   English

C如何防止文件被覆盖

[英]C How to stop file from being overwritten

Currently the the file gets created and overwritten. 当前,文件已创建并覆盖。 I am trying to get it so that if the file already exists it just exits the program. 我正在尝试获取它,以便如果该文件已经存在,它将退出程序。 open must be used. 必须使用open。

if ((dest = open(argv[2], O_WRONLY | O_CREAT, 0644)) == -1) {
    printf("Error File %s exists", argv[2]);
    return 3; 
 }

Just use O_EXCL : 只需使用O_EXCL

O_EXCL Ensure that this call creates the file: if this flag is specified in conjunction with O_CREAT , and pathname already exists, then open() will fail . O_EXCL确保此调用创建文件:如果将此标志与O_CREAT一起指定, 并且路径名已存在,则open()将失败

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM