By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,601 Members | 989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,601 IT Pros & Developers. It's quick & easy.

help please: insert string into file at specific location

P: n/a
I want to open a file and write some string between the the 3rd line and the
4th line. my code (as follows) can insert after the 3rd line but somehow it
owerwrites the 4th line. So i lost its original content. As my original file has
more than 10000 lines of code, I'm looking for a handy way to do that. Does
anyone know how I can fix it? Please help me. Thanks very much.
$content = "new content here\n"
$handle = fopen("test.wrl", "r+"); //open file
$theData = "";
$i = 1;
while($i<4){
$theData += fgets($handle);
$i++;
}
fputs($handle, $content); //write
fclose($handle); //close it
Nov 1 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
help wrote:
I want to open a file and write some string between the the 3rd line and
the 4th line. my code (as follows) can insert after the 3rd line but
somehow it owerwrites the 4th line. So i lost its original content. As my
original file has
more than 10000 lines of code, I'm looking for a handy way to do that.
Does anyone know how I can fix it? Please help me. Thanks very much.
$content = "new content here\n"
$handle = fopen("test.wrl", "r+"); //open file
$theData = "";
$i = 1;
while($i<4){
$theData += fgets($handle);
$i++;
}
fputs($handle, $content); //write
fclose($handle); //close it


Hi,

AFAIK fopen doesn't have an 'insert'-mode.
Probably the easiest way to code this is to read the whole file in memory,
and write it back.
Have a look at the function file() at www.php.net which returns an array of
all lines.

Regards,
Erwin Moller
Nov 1 '05 #2

P: n/a
VS
help wrote:
I want to open a file and write some string between the the 3rd line and the
4th line. my code (as follows) can insert after the 3rd line but somehow it
owerwrites the 4th line. So i lost its original content. As my original file has
more than 10000 lines of code, I'm looking for a handy way to do that. Does
anyone know how I can fix it? Please help me. Thanks very much.
$content = "new content here\n"
$handle = fopen("test.wrl", "r+"); //open file
$theData = "";
$i = 1;
while($i<4){
$theData += fgets($handle);
$i++;
}
fputs($handle, $content); //write
fclose($handle); //close it


How about something like this (untested - typed live):

function insert_line($text, $pos){

$file = "test.wrl";
$tmp = "new.wrl";

$old = fopen($file, "r"); // Open File
if ( ! $old ){
return 0;
}else{
$new = fopen($tmp, "w+"); // Create New
if( ! $new ){
return 0;
}

$l=0;
while( !feof($old) ){
$line = fgets($old, 512);
if( $l == $pos ){
fputs($new, $text);
}
fputs($new, $line);
$l++;
}
fclose($old);
fclose($new);

if( ! copy($tmp, $file) ){
return 0;
}

unlink($tmp);
return 1;
}
}

--
VS
Nov 1 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.