Appending to the body of a file
Posted: Mon Jun 02, 2003 8:26 pm
Is there a relatively simple way to add lines to a file at a specific point without destroying any other data in the file?
I am basically wanting to insert a new line of text into a file so that the lines stay in decending alphabetical order in reference to a specific word in each line.
I tried this code with no luck:
mod_edit: added
I am basically wanting to insert a new line of text into a file so that the lines stay in decending alphabetical order in reference to a specific word in each line.
I tried this code with no luck:
Code: Select all
function find_alpha_pos($target, $file, $delim)
{
while (!feof($file))
{
// record read position
$read_pos = ftell($file);
// read line from file
$line = fgets($file);
$parts = explode($delim, $line);
if ($parts[1] == "")
{
$name = $parts[0];
}
else
{
$name = $parts[1];
}
if ($name < $target) continue;
else
{
fseek($file, $read_pos);
break;
}
}
}Code: Select all
tags[/size]