为什么fwrite以后,fread不出来东西呢
程序员文章站
2022-06-08 16:24:40
...
为何fwrite以后,fread不出来东西呢。
$file = 'test.txt';
$fileHandle = fopen($file,'w+');
fwrite($fileHandle, 'Newly inserted text');
$fileContents = fread($fileHandle, filesize($file));
echo $fileContents;
fclose($fileHandle);
The manual says that if the w+ option is specified then the file is opened for reading and writing. From what I understand this means that I should be able to read from the file after writing to it. But the code above displays nothing after writing to the file. The file is chmodded 777. Can someone please explain to me why this is so :D
同问,为啥呢。
以w+模式打开,应该是可读,
可为何fwrite以后,fread不出来东西呢。
------解决方案--------------------
还真没试过这样,但可以猜想一下
1.指针问题
2.IO问题
想想写完之后指针在哪?如果在文件开头,第二次写入不是写到第一次前面了?
文件IO一般都涉及缓冲,两次操作中间无间断总会有想象不到的问题……
$file = 'test.txt';
$fileHandle = fopen($file,'w+');
fwrite($fileHandle, 'Newly inserted text');
$fileContents = fread($fileHandle, filesize($file));
echo $fileContents;
fclose($fileHandle);
The manual says that if the w+ option is specified then the file is opened for reading and writing. From what I understand this means that I should be able to read from the file after writing to it. But the code above displays nothing after writing to the file. The file is chmodded 777. Can someone please explain to me why this is so :D
同问,为啥呢。
以w+模式打开,应该是可读,
可为何fwrite以后,fread不出来东西呢。
------解决方案--------------------
还真没试过这样,但可以猜想一下
1.指针问题
2.IO问题
想想写完之后指针在哪?如果在文件开头,第二次写入不是写到第一次前面了?
文件IO一般都涉及缓冲,两次操作中间无间断总会有想象不到的问题……
相关文章
相关视频
专题推荐
-
独孤九贱-php全栈开发教程
全栈 170W+
主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门
-
玉女心经-web前端开发教程
入门 80W+
主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门
-
天龙八部-实战开发教程
实战 120W+
主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论1 条评论非常感谢解答。