Bug #80088 [Opn->Nab]: c+ open mode messes up file on fwrite()
| From: | requinix@php.net | Date: | Thu, 10 Sep 2020 22:28:10 +0000 |
| Subject: | Bug #80088 [Opn->Nab]: c+ open mode messes up file on fwrite() | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-228967@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80088&edit=1
ID: 80088
Updated by: requinix@php.net
Reported by: smitty7523 at yahoo dot com
Summary: c+ open mode messes up file on fwrite()
-Status: Open
+Status: Not a bug
Type: Bug
Package: Filesystem function related
Operating System: Linux Manjaro
PHP Version: 7.4.10
Block user comment: N
Private report: N
New Comment:
File writes do not insert. That is not how files work.
If you want to insert then you have to read in everything that comes at and after your
"insertion" point, write what you want to add, and then write out the rest of what was
there.
Previous Comments:
------------------------------------------------------------------------
[2020-09-10 22:19:20] smitty7523 at yahoo dot com
Description:
------------
I am trying to insert a line in a file after a line that is searched for.
The line to insert is inserted fine, but the text after it gets truncated partially.
********* test.txt ************
PHP is a great language.
It is a tool for the ages.
It is easy to use and fast too!
Test script:
---------------
$absoluteFilePath = '<your_root_path>/test.txt';
$splFile = new \SplFileObject($absoluteFilePath, 'c+');
$splFile->flock(LOCK_EX);
$splFile->rewind();
// Add to next line.
while ($splFile->valid()) {
$currentLine = $splFile->fgets();
if (strpos($currentLine, "It is a tool for the ages.") !== false) {
$splFile->fwrite("But it has a few bugs.\n");
}
}
$splFile->flock(LOCK_UN);
Expected result:
----------------
PHP is a great language.
It is a tool for the ages.
But it has a few bugs.
It is easy to use and fast too!
Actual result:
--------------
PHP is a great language.
It is a tool for the ages.
But it has a few bugs.
ast too!
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80088&edit=1