我想用fopen的w+模式,但是发现不能读文件,只能写,这可把我郁闷了,r+是可以读可以写,但是每次写的时候,文件指针总是在文件结尾。x+也不行,直接报错,我用@抑制也不好使。最好能告诉我w+为啥不能读。

解决方案 »

  1.   

    w+可以读啊r   Read only. Starts at the beginning of the file
    r+  Read/Write. Starts at the beginning of the file
    w  Write only. Opens and clears the contents of file; or creates a new file if it doesn't exist
    w+  Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist
    a  Append. Opens and writes to the end of the file or creates a new file if it doesn't exist
    a+  Read/Append. Preserves file content by writing to the end of the file
    x  Write only. Creates a new file. Returns FALSE and an error if file already exists
    x+  Read/Write. Creates a new file. Returns FALSE and an error if file already exists
      

  2.   

    w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn't exist注意,w+是会清空文件内容的
      

  3.   

    我在写入文件之前,先做了个读取文件操作,但是返回的全是false,然后才做的fwrite,怪了啊
      

  4.   

    while (!feof($handle)) {
                    $flag = fgets($handle); //这里就读取不出来
                    if ($num = strpos($flag, ']')) {  //如果是目录名称
                        $cname = substr($flag, 2, $num-2); //获取目录名称
                        $arr[$cname] = array(); //将目录名称定义为第二维数组的下标
                    } else {
                        $arr[$cname][] = $flag;  //否则就将其内容放到上一个目录下
                    }
                }
      

  5.   

    从读取到写入:
      $dirPath = SVN_ROOT.ltrim(strrchr($data['pname'], '/'), '/');
            if (!file_exists($dirPath)) {
                mkdir($dirPath);
            }
            if (!file_exists($dirPath.'/authz')) {
                $handle = fopen($dirPath.'/authz', 'w+b');
                $write  = "[/" . $data['cname'] . "]\r\n";
                $write .= "@" . $data['gname'] . " = " .str_replace('n', '', $data['auth'])."\r\n";
            } else {
                $handle = @fopen($dirPath.'/authz', 'w+b');
                $arr    = array();
                while (!feof($handle)) {
                    $flag = fgets($handle); //将authz文件里的每行记录都读取出来
                    if ($num = strpos($flag, ']')) {  //如果是目录名称
                        $cname = substr($flag, 2, $num-2); //获取目录名称
                        $arr[$cname] = array(); //将目录名称定义为第二维数组的下标
                    } else {
                        $arr[$cname][] = $flag;  //否则就将其内容放到上一个目录下
                    }
                }
                
                $count = count($arr[$data['cname']]);
                $arr[$data['cname']][$count] = '@'.$data['gname'] . ' = ' . str_replace('n', '', $data['auth']);
                $write = '';
                foreach ($arr as $key => $value) {
                    if (!empty($key)) {
                        $write .= '[/' . $key ."]\r\n";
                    }
                    foreach ($value as $v) {
                        if ($v != "") {
                            $write .= $v."\r\n";
                        }
                    }
                }
            }
            fwrite($handle, $write);
            fclose($handle);
      

  6.   

    你用 w+ 打开文件时,文件就被清空了如果你是想修改文件,应该用 r+ 打开文件
    你可以用fseek 移动文件指针
      

  7.   

    是要这个么?
    function writeover($filename,$data,$method="rb+",$iflock=1,$check=1,$chmod=1){
    touch($filename);
    $handle = fopen($filename,$method);
    $iflock && flock($handle,LOCK_EX);
    fwrite($handle,$data);
    $method=="rb+" && ftruncate($handle,strlen($data));
    fclose($handle);
    $chmod && @chmod($filename,0777);
    }
    function readover($filename,$method='rb',$readsize='D'){
    $filedata="";
    if(file_exists($filename)){
    $filesize = @filesize($filename);
    $readsize!='D' && $filesize = min($filesize,$readsize);
    $filedata = '';
    if ($handle = @fopen($filename,$method)) {
    flock($handle,LOCK_SH);
    $filedata = @fread($handle,$filesize);
    fclose($handle);
    }
    }
    return $filedata;
    }
      

  8.   

    $file = fopen("test.txt","r+");
    // read
    fgets($file);
    // move back to beginning of file
    fseek($file,0);