如何选择开放模式与fopen()? [英] How to choose open mode with fopen()?

查看:152
本文介绍了如何选择开放模式与fopen()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 fopen()来解决文件打开的问题。

自由寻找游标,有时甚至可能超过 EOF 。更重要的是,我也想追加到它而不是截断现有的文件。我试图打开 a + 模式下的文件;但是,我不能自由地寻找文件光标。每当我寻找超出 EOF 的游标时,新到达的数据将被追加到文件的末尾,而不是我指定的位置。而如果在 w + 模式下打开,现有文件将被截断。有没有一个完美的解决这个问题?






更新:

<没有明确规定的一点是,文件可能并不总是存在;在这种情况下,我必须创建一个新文件。



实际上,我想处理一个配置文件。我不知道这是否是好的做法。或者我应该先放置一个空的配置文件。因此,有没有必要关心案件文件不存在?



以下是代码片段:

  FILE * f = fopen(FILE_PATH,wb +); 
struct record r; (f){
if(fread((void *)& r,1,sizeof(struct record),f){
/ *文件存在,执行正常流程* /
} else {
if(feof(f)){
/ *文件是新创建的,执行一些初始化* /
}
}
} else {
/ *发出警告* /
}


解决方案

您必须在两个阶段处理可能不存在的文件,首先假定它在那里,然后处理它的缺席:

<如果(f = fopen(filename,rb +)== 0)
f = fopen(filename,wb +);
if(f = = 0)
...报告错误...



rb +模式将无法打开一个不存在的文件(但其他情况下会按照您的需要运行)如果该文件不存在,那么wb +会做你想要的东西(虽然它可能仍然失败,例如,如果文件存在但你没有写权限你必须希望你没有受到TOCTOU(检定时间,使用时间)攻击的双重打击。

另一种选择方法使用具有适当标志的 open()系统调用的3参数版本来打开文件描述符,然后使用 fdopen() code>从文件描述符中创建一个文件流:

$ p $ #include< fcntl.h>

int fd; (fd = open(filename,O_RDRW | O_CREAT,0644))> = 0)
f = fdopen(fd,rb +)

您对 open()


I have a problem about file open mode with fopen().

In my case, I want to seek the cursor freely, may be even beyond EOF sometimes. What's more, I also want to append to it instead of truncating the existing file. I tried to open the file in a+ mode; however, I cannot seek file cursor freely. Each time I seek the cursor beyond EOF, new arrival data will be append to end of the file, not the position I specified. While, if open in w+ mode, the existing file will be truncated. Is there a perfect solution to this problem?


UPDATE:

One point not clearly specified is that the file may not always exist; I have to create a new file in that case.

In fact, I want to process a configuration file. I don't know whether it is good practice to implement like this. Or should I first place an empty configuration file. Thus there is no need to care about the case file does not exist?

Below is the code snippet:

FILE *f = fopen(FILE_PATH, "wb+");
struct record r;
if (f) {
    if (fread((void *)&r, 1, sizeof(struct record), f) {
        /* File exists, do the normal flow */
    } else {
        if (feof(f)) {
            /* File is newly created, do some initialization */
        }
    }
} else {
    /* issue a warning */
}

解决方案

You will have to deal with the possibly non-existent file in two stages, first assuming that it is there and then dealing with its absence:

if ((f = fopen(filename, "rb+") == 0)
    f = fopen(filename, "wb+");
if (f == 0)
    ...report error...

The "rb+" mode will fail to open a non-existent file (but otherwise behaves as you want). If the file doesn't exist, then "wb+" will do what you want instead (though it could still fail, for example if the file exists but you don't have permission to write to it). You have to hope that you've not been subjected to a TOCTOU (Time of Check, Time of Use) attack with the double attempt.

An alternative approach uses the 3-argument version of the open() system call with appropriate flags to open a file descriptor, and then uses fdopen() to create a file stream from the file descriptor:

#include <fcntl.h>

int fd;
if ((fd = open(filename, O_RDRW | O_CREAT, 0644)) >= 0)
    f = fdopen(fd, "rb+");

You get rather precise control over open() with the flags.

这篇关于如何选择开放模式与fopen()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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