Results 1 to 2 of 2

Thread: Function to input a string into a text file, not append

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2004
    Location
    NYS
    Posts
    29

    Default Function to input a string into a text file, not append

    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."
    ";
    	$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");
    }
    I'm having a problem though, in my get_position statement, my while loop is infinite for some reason. Anyone know why?

    That should be the only problem i have... if you want to test the script, go to dburg-php.bizhat.com, log in with tom password hhh go to classwork and submit something.

    sdb.txt is where it will be written to.

    my dbase mng ment file is

    Code:
    <html>
    <body>
    <?php
    
    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']."
    ";
    		while(!(feof($s)))
    		{
    			$name=stream_get_line($s,"2048","รพ")."\n";
    			echo "Comparing database name \"".$name."\"to username \"".$_COOKIE['uname']."\"
    "; 
    			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;
    }
    
    
    ?>
    </body>
    </html>
    [/code]

  2. #2
    Join Date
    Nov 2004
    Location
    NYS
    Posts
    29

    Default

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •