简体   繁体   English

fopen 函数中的 a 和 a+ 选项有什么区别?

[英]What is the difference between a and a+ option in fopen function?

I can't understand the description of the "a" and "a+" option in the C fopen api documentation.我无法理解 C fopen api 文档中“a”和“a+”选项的描述。 The option in "a+" is append and update. “a+”中的选项是追加和更新。 What is the meaning of the word update here?这里的更新这个词是什么意思?

Here is what the man pages ( man fopen ) say: 这是man page( man fopen )所说的:

a 一种

Open for appending (writing at end of file). 打开附加(在文件末尾写)。 The file is created if it does not exist. 如果文件不存在,则创建该文件。 The stream is positioned at the end of the file. 流位于文件的末尾。

a+ A +

Open for reading and appending (writing at end of file). 打开阅读和追加(在文件末尾写)。 The file is created if it does not exist. 如果文件不存在,则创建该文件。 The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file. 用于读取的初始文件位置位于文件的开头,但输出始终附加到文件的末尾。


Which means: 意思是:

for a+: pointer initially is at the start of the file ( for reading ) but when a write operation is attempted it is moved to the end of the file. 对于a +:指针最初位于文件的开头(用于读取),但是当尝试写入操作时,它会移动到文件的末尾。

Yes, there is an important difference: 是的,有一个重要的区别:

a : append data in a file, it can update the file writing some data at the end; a :将数据附加到文件中,它可以更新文件末尾写入一些数据;

a+ : append data in a file and update it, which means it can write at the end and also is able to read the file. a + :将数据附加到文件中并更新它,这意味着它可以在最后写入并且还能够读取文件。

In a pratical situation of only writing a log both are suitable, but if you also need to read something in the file (using the already opened file in append mode) you need to use " a+ ". 在只写日志的实际情况下,两者都是合适的,但是如果你还需要读取文件中的某些内容 (使用已经打开的附加模式文件),则需要使用“ a + ”。

check the following code检查以下代码

<?php 
function find_user($email){
    $f = "readme.txt";
    $file = fopen($f,'r');
    while(!feof($file)){
        $arr = explode(",",fgets($file));
        if(array_search($email,$arr) === 0){
            return true;
            break
        }
        }
        fclose($file);
return false;
    }

    function check_name($name){
        $f = "readme.txt";
        $file = fopen($f,'r');
        while(!feof($file)){
            $arr = explode(",",fgets($file));
            if(array_search($name,$arr) === 3){
                return true;
                break
            }
            }
            fclose($file);
    return false;
    }

function check($email,$password){
    $f = "readme.txt";
    $file = fopen($f,'r');
    while(!feof($file)){
        $arr = explode(",",fgets($file));
        if(array_search($email,$arr) === 0 && array_search($password,$arr) === 1){
            return true;
            break
        }
        }
        fclose($file);
return false;
}
?>

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

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