PDA

View Full Version : fseek question


scottkenyon
05-30-2005, 03:24 PM
can someone please list an example of how i can use fseek to:

1. find a particular variable (in this case, $gigDat, as part of a line within a txt file
2. go to the beginning of that line
3. overwrite the entire line with a new line

here's what i've got so far, i'm not sure where to go next.

<?php

$gigNum = $_POST['gignum'];
$gigDat = $_POST['gigdat'];
$gigLoc = $_POST['gigloc'];
$gigURL = $_POST['gigurl'];
$gigDet = $_POST['gigdet'];
$toSave = "&gigNum$gigNum=$gigNum&gigTit$gigNum=$gigTit&gigDat$gigNum=$gigDat&gigLoc$gigNum=$gigLoc&gigURL$gigNum=$gigURL&gigDet$gigNum=$gigDet\r";

$fp = fopen("gigs.txt", "r+"); //file exists, open it for reading/writing/and seeking
fseek($fp, -9, [$gigDat]);//move the pointer to the specified gigDat, then backwards 9 characters (9 being the length of "&gigNum=*").

if(fwrite($fp, $toSave)){;
echo("writing=Ok&");
}else{
echo("writing=Error&");
}
fclose($fp);
?>

madgett
06-04-2005, 08:01 PM
Do you only need to change the last recorded line in the file? I'm not sure what the value of [$gigDat] is but what you can do is set that third parameter to SEEK_END then just work backwards from the end of the file. If you know the length of the last saved line of text, this should be fairly straightforward.

mmm..pi..3.14..
06-04-2005, 10:26 PM
lol, didn't I help you with that PHP script?? For some reason it looks familiar :rolleyes:

scottkenyon
06-04-2005, 11:05 PM
mmmpi..

yes, you did help me with that! thanks again.
i wanted to try and get more done with it, but, i've abandoned that avenue for mySQL, which i've spent all day learning. i think it can do the job i want much better!

ps, since I mentioned it, do you know of any good tutorial or info i can use to learn how to get my mySQL data back into flash? should i use xml (which I don't know), or can i come straght out of my SQL?

mmm..pi..3.14..
06-05-2005, 03:51 AM
you need to go through PHP or ASP to get flash to interact with MySQL. XML does not actually do anything with MySQL. XML is just a way of formatting data, it can't do anything else like read or write to a text file. XML is just a fancy form of a text file, with the exception that it has a "standard", so all XML files are similar in one way or another.

About your PHP problem...how are you writing the lines? So I can write a sample PHP script for you. Are you separating the lines with "\n" or with "\r\n". I'd use "\r\n" since it actually does real line breaks in text files in windows, instead of just placing a weird "box" character in between each line of text :)

Eric

scottkenyon
06-05-2005, 04:09 PM
ok, i'll try and use php to get the data from mySQL...

yes, i'm formatting with /r/n on the end of each line...

mmm..pi..3.14..
06-06-2005, 07:45 PM
Try this Scott. First time you run the script it will create a text file with 10 lines of text. The second time you run it, it will find line # 8, and overwrite the entire line with something new, which can be changed in the variable "$Replacement". You'll just need to do some modifying to get it to handle GET or POST, which shouldn't be hard at all for you.

Keep in mind that after it overwrites the old text, the script stops, so as to not over write any other lines that might contain the same text. So if your file looked like:

Line # 8
Line #18

and you wanted to replace the line that had a number "8" in it, the PHP script would only overwrite "Line # 8", and "Line # 18" would remain the same.

<?php
$Search = "8";//look for the line with the number 8 in it
$Replacement = "This is a replacement line for line number 8";//what to replace the line with
$Offset = 0;
if(!file_exists("Gigs.txt")){
$File = fopen("Gigs.txt", "w+");
for($i=0; $i<11; $i++){
fwrite($File, "Line # " . $i . "\r\n");
}
}else{
$File = fopen("Gigs.txt", "r+");
while(!feof($File)){
$Line = fgets($File);
if(stristr($Line, $Search) == true){
$Ending = fread($File, 1024);
rewind($File);
fseek($File, $Offset, SEEK_SET);
fwrite($File, $Replacement . "\r\n" . $Ending);
break;
}
$Offset += strlen($Line);
}
}
fclose($File);
?>

Eric :D