Valhalla Legends Forums Archive | Web Development | A function to insert a string in a file, NOT append...

AuthorMessageTime
Black4C6F747573
I'm writing a function to place a string in the middle of a file.  Say you use code similar to
[code] while(!(feof($s)))
{
$name=stream_get_line($s,"2048","þ")."\n";
if($name===$_COOKIE["uname"])
{
                              some code
break;
}
fgets($s);
}[/code]

to search a text file for some name , then say you find the name and want to put some information after it to be read by another file.  Well, fwrite would write OVER any information after the insertion point, messing up other names.
My function will read everything after the insertion point, put it in a var, and return to the insertion point, then insert the string u want, and re-write the rest of the file.

my current code is

[code]function get_position($file)
{
while(!(feof($file)))
{
fseek($file,1,SEEK_CUR);
$offset++;
if(feof($file))
break;
}
rewind($file);
while(!(feof($file)))
{
fseek($file,1,SEEK_CUR);
$total++;
if(feof($file))
break;
}
$position=$total-$offset;
return($position);
}



function put_str_fp($file,$str){
echo $str."<br>";
$pos=get_position($file);
while(!(feof($file)))
{
$buf.=fgetc($file);
}
echo $buf;
fseek($file,$pos,"SEEK_SET");
fwrite($file,$str);
fwrite($file,$buf);
return("Success");
}[/code]

exactly,  but when, in my main file, i call put_str_fp(), the while loop in get_position becomes infinite and I can't figgure out why.  Any help would be appreciated.  HEre is my main code if you want to know.

[code]
include "functions.php";

if(!($_SERVER['HTTP_REFERER']==='http://dburg-php.bizhat.com/teacher-consol.php'))
exit('You can not access this page.  Please log into your console and edit the database that way.');

switch($_POST[f_type]) {

case school:

if(!($s=fopen('sdb.txt','r+')))
  exit('Unable to open schoolwork database file.');
echo "Checking Database for username: ".$_COOKIE['uname']."<br>";
while(!(feof($s)))
{
$name=stream_get_line($s,"2048","þ")."\n";
echo "Comparing database name \"".$name."\"to username \"".$_COOKIE['uname']."\"<br>";
if($name===$_COOKIE["uname"])
{
echo "Found ".$_COOKIE['uname']." in database, writing information.";
fseek($s,1,SEEK_CUR);
$t_input="|$_POST[month]-$_POST[day]|$_POST[topic]|$_POST[subject]|\n";
echo put_str_fp($s,$t_input);
break;
}
fgets($s);
}

/* while(!(ord(fgetc($s))==='254'))
{
  fseek($s,-1,SEEK_CUR);
  fwrite($s,'');
}
*/ break;
case home:
echo 'home right';
  if(!($h=fopen('hdb.txt','r+')))
      exit('Unable to open homework database file.');
break;
default:
echo 'This page was not retrieved correctly. Please re-log into your console and try again.';
break;
}

[/code]

btw this is php, not sure if I said that.
December 3, 2004, 12:54 AM
Black4C6F747573
wee, two days later...

for some reason, my fseek() wasn't being processed every loop, so I changed the fseek to a fgetc and it functions the same way.

I'll post the new function for anyone who wants it in a say or so after I get rid of all the bugs.  I will make a complete database script as well for anyone who doesn't like mySQL or does not have a localhost/server that supports it.

December 3, 2004, 3:36 PM
Myndfyr
Quick thought ---

You might want to, instead of reading *everything* into a variable, to block transfers of some number.  For instance, make the file longer by however long the data is you need to insert, then copy in increments of some constant number of bytes.  So for instance, let's say your constant is 2 (that's silly, but I'm too lazy to make a larger example), you want to insert 4 bytes, say AAAA, into this eleven-byte file at index 2:

XXUFMDSERF\0

You first append four bytes:
XXUFMDSERF\0\0\0\0\0

Move 2 at a time (2 being your given constant):
XXUFMDSERF\0\0RF\0
XXUFMDSERFSERF\0
XXUFMDSEMDSERF\0
XXUFMDUFMDSERF\0

Then you write your four bytes (in this case) into your stream at the insertion point:
XXAAAAUFMDSERF\0

Doing it this way saves memory.  It's slower, but the speed is inversely related to the amount of memory in your buffer.
December 6, 2004, 11:15 PM
Black4C6F747573
thanks for the information.  I will definatly take this into consideration :-)
December 8, 2004, 3:35 PM
Black4C6F747573
[quote author=MyndFyre link=topic=9755.msg91259#msg91259 date=1102374950]
Quick thought ---

You might want to, instead of reading *everything* into a variable, to block transfers of some number.  For instance, make the file longer by however long the data is you need to insert, then copy in increments of some constant number of bytes.  So for instance, let's say your constant is 2 (that's silly, but I'm too lazy to make a larger example), you want to insert 4 bytes, say AAAA, into this eleven-byte file at index 2:

XXUFMDSERF\0

You first append four bytes:
XXUFMDSERF\0\0\0\0\0

Move 2 at a time (2 being your given constant):
XXUFMDSERF\0\0RF\0
XXUFMDSERFSERF\0
XXUFMDSEMDSERF\0
XXUFMDUFMDSERF\0

Then you write your four bytes (in this case) into your stream at the insertion point:
XXAAAAUFMDSERF\0

Doing it this way saves memory.  It's slower, but the speed is inversely related to the amount of memory in your buffer.
[/quote]

I got thinking about your idea, but I couldn't find a function to do it.  Which would I use?  and would I be appending 4 null bytes? it seems in your example you appened SERF to the end of XXUFMDSERF, only because I can't figgure out where the 4 \0's went after you appended those or where the extra RF came from after you moved the first two bytes.  It's a good idea though, it might help me with a different problem im having.  :-)
December 9, 2004, 3:09 PM
Myndfyr
You just copy the data that you're moving over the old data.
December 9, 2004, 9:14 PM

Search