• Welcome to Valhalla Legends Archive.
 

A function to insert a string in a file, NOT append...

Started by Black4C6F747573, December 02, 2004, 06:54 PM

Previous topic - Next topic

Black4C6F747573

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


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

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");
}


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.


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;
}



btw this is php, not sure if I said that.

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.


MyndFyre

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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Black4C6F747573

thanks for the information.  I will definatly take this into consideration :-)

Black4C6F747573

Quote from: MyndFyre on December 06, 2004, 05:15 PM
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.

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.  :-)

MyndFyre

You just copy the data that you're moving over the old data.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.