Jump to content

fwrite to specific pointer position


dodgeitorelse3

Recommended Posts

Hi folks,

I am stumped as to why I cannot write data to a specific pointer position in an existing file.

The file I want to write to contains ...

 

****many other lines are here, removed to shorten for this post****

tooltips[304]=["/mapimages/tn_spider1.jpg", "Operation_BlackSpider120<br><br>Objectives:<br>A: Make contact with the Black Spider<br>B: Locate the case that has the laptop location<br>C: Locate and hack the laptop<br>D: Use access code and release the prissoners<br>E: Move out and locate the extractionpoint <br><br>Terrain: Snow and Hills<br><br>Creator: SF{Ops}PatFinder<br><br>Modder: <br><br>Mods: ", {background:"gray", border:"none", font:"bold 12px Arial", color:"black"}]

tooltips[305]=["/mapimages/tn_296_nomap.jpg", "The_Observatory_PF<br><br>Objectives:<br>A: Infiltrate the Observatory Complex<br>B: Locate and secure experimental communications data<br>C: Disable the laser weapon system<br>D: Restore power to sattellite communication system<br>E: Restore main power grid<br>F: Radio for extraction<br>G: Locate extraction point<br><br>Terrain: In and outside buildings<br><br>Creator: -{KWK}-Kman<br><br>Modder: SF{Ops}PatFinder<br><br>Mods: ", {background:"gray", border:"none", font:"bold 12px Arial", color:"black"}]

tooltips[306]=["/mapimages/tn_296_nomap.jpg", "The_Breach<br><br>Objectives:<br>A: <br>B: <br>C: <br>D: <br>E: <br>F: <br>G: <br><br>Terrain: <br><br>Creator: -{KWK}-Kman<br><br>Modder: <br><br>Mods: ", {background:"gray", border:"none", font:"bold 12px Arial", color:"black"}]

tooltips[307]=["/mapimages/tn_radio1.jpg", "SFRadioTower<br><br>Objectives:<br>A: Disable the radiotower antenna generator<br>B: Rescue AID worker<br>C: Destroy the attack helicopter<br>D: Destroy the weapon cache<br><br>Terrain: <br><br>Creator: 151Alpha<br><br>Modder:<br><br>Mods: ", {background:"gray", border:"none", font:"bold 12px Arial", color:"black"}]

tooltips[308]=["/mapimages/tn_agency.jpg", "The_Agency<br><br>Objectives:<br>A: Infiltrate the intelligence facility (7x)<br>B: Locate and question the analist<br>C: Secure the stolen intel<br>D: Access the mainframe to stop any intel transfer<br>E: Locate extraction point<br><br>Terrain: <br><br>Creator: -{KWK}-Kman<br><br>Modder:<br><br>Mods: ", {background:"gray", border:"none", font:"bold 12px Arial", color:"black"}]

return tooltips //do not remove/change this line
}(),
tooltipoffsets: [20, -30], //additional x and y offset from mouse cursor for tooltips
//***** NO NEED TO EDIT BEYOND HERE
tipprefix: 'imgtip', //tooltip ID prefixes
createtip:function($, tipid, tipinfo){
if ($('#'+tipid).length==0){ //if this tooltip doesn't exist yet
return $('<div id="' + tipid + '" class="ddimgtooltip" />').html(
'<div style="text-align:center"><img src="spacer.jpg" data-src="' + tipinfo[0] + '" /></div>'
+ ((tipinfo[1])? '<div style="text-align:left; margin-top:5px">'+tipinfo[1]+'</div>' : '')
)
.css(tipinfo[2] || {})
.appendTo(document.body)
}
return null
},
positiontooltip:function($, $tooltip, e){
var x=e.pageX+this.tooltipoffsets[0], y=e.pageY+this.tooltipoffsets[1]
var tipw=$tooltip.outerWidth(), tiph=$tooltip.outerHeight(),
x=(x+tipw>$(document).scrollLeft()+$(window).width())? x-tipw-(ddimgtooltip.tooltipoffsets[0]*2) : x
y=(y+tiph>$(document).scrollTop()+$(window).height())? $(document).scrollTop()+$(window).height()-tiph-10 : y
$tooltip.css({left:x, top:y})
},

showbox:function($, $tooltip, e){
var $tipimage=$tooltip.find('img:eq(0)')
if (!$tipimage.attr('data-srcloaded'))
$tipimage.attr({'src':$tipimage.attr('data-src'), 'data-srcloaded':1})
$tooltip.show()
this.positiontooltip($, $tooltip, e)
},
hidebox:function($, $tooltip){
$tooltip.hide()
},

init:function(targetselector){
jQuery(document).ready(function($){
var tiparray=ddimgtooltip.tiparray
var $targets=$(targetselector)
if ($targets.length==0)
return
var tipids=[]
$targets.each(function(){
var $target=$(this)
$target.attr('rel').match(/\[(\d+)\]/) //match d of attribute rel="imgtip[d]"
var tipsuffix=parseInt(RegExp.$1) //get d as integer
var tipid=this._tipid=ddimgtooltip.tipprefix+tipsuffix //construct this tip's ID value and remember it
var $tooltip=ddimgtooltip.createtip($, tipid, tiparray[tipsuffix])
$target.mouseenter(function(e){
 var $tooltip=$("#"+this._tipid)
 ddimgtooltip.showbox($, $tooltip, e)
})
$target.mouseleave(function(e){
 var $tooltip=$("#"+this._tipid)
 ddimgtooltip.hidebox($, $tooltip)
})
$target.mousemove(function(e){
 var $tooltip=$("#"+this._tipid)
 ddimgtooltip.positiontooltip($, $tooltip, e)
})
if ($tooltip){ //add mouseenter to this tooltip (only if event hasn't already been added)
 $tooltip.mouseenter(function(){
 ddimgtooltip.hidebox($, $(this))
 })
}
})
}) //end dom ready
}
}
//ddimgtooltip.init("targetElementSelector")
ddimgtooltip.init("*[rel^=imgtip]")

 

I want to write to this file at the space before the line containing "return tooltips" without the quotes. Note that line is 71 lines from the bottom.

 

This is the code I use to write to the file ....

 

<?php
echo("Tooltip ID Number from db: " . $_POST['tooltip_number'] . "<br />\n");
echo("Thumbnail name for image: " . $_POST['tn_image'] . "<br />\n");
echo("Map Name: " . $_POST['map_name'] . "<br />\n");
echo("Objective A: " . $_POST['obj_a'] . "<br />\n");
echo("Objective B: " . $_POST['obj_b'] . "<br />\n");
echo("Objective C: " . $_POST['obj_c'] . "<br />\n");
echo("Objective D: " . $_POST['obj_d'] . "<br />\n");
echo("OTerrain: " . $_POST['terrain'] . "<br />\n");
echo("Creator: " . $_POST['creator'] . "<br />\n");
echo("Modder: " . $_POST['modder'] . "<br />\n");
echo("Mods: " . $_POST['mods'] . "<br />\n");

echo "<br /><br /><br />";

$new_tt = "\r\n\r\n	 tooltips[" . $_POST['tooltip_number'] . "]=[\"/mapimages/" . $_POST['tn_image'] . "\", \"" . $_POST['map_name'] . "<br><br>Objectives:<br>A: " . $_POST['obj_a'] . "<br>B: " . $_POST['obj_b'] . "<br>C: " . $_POST['obj_c'] . "<br>D: " . $_POST['obj_d'] . "<br><br>Terrain: " . $_POST['terrain'] . "<br><br>Creator: " . $_POST['creator'] . "<br><br>Modder: " . $_POST['modder'] . "<br><br>Mods: " . $_POST['mods'] . "\", {background:\"gray\", border:\"none\", font:\"bold 12px Arial\", color:\"black\"}]\r\n\r\n";

echo "$new_tt";

$key = 'return tooltips';
//copy file to prevent double entry
$file = "test_fwrite_file.js";
$newfile = "test_fwrite_file.txt";
copy($file, $newfile) or exit("failed to copy $file");
//load file into $lines array
$fc = fopen ($file, "r");
while (!feof ($fc))
{
$buffer = fgets($fc, 4096);
$lines[] = $buffer;
}
fclose ($fc);
//open same file and use "w" to clear file
$f=fopen($newfile,"w") or die("couldn't open $file");
/* uncomment to debug
print_r($lines);
print "<br>\n";
*/
//loop through array using foreach
foreach($lines as $line)
{
 fwrite($f,$line); //place $line back in file
if (strstr($line,$key)){ //look for $key in each line
fwrite($f,$new_tt);
} //place $line back in file
}
fclose($f);
copy($newfile, $file) or exit("failed to copy $newfile");
?>

 

however when it writes to the file it places it after the $key such as

 

tooltips[308]=["/mapimages/tn_agency.jpg", "The_Agency<br><br>Objectives:<br>A: Infiltrate the intelligence facility (7x)<br>B: Locate and question the analist<br>C: Secure the stolen intel<br>D: Access the mainframe to stop any intel transfer<br>E: Locate extraction point<br><br>Terrain: <br><br>Creator: -{KWK}-Kman<br><br>Modder:<br><br>Mods: ", {background:"gray", border:"none", font:"bold 12px Arial", color:"black"}]

return tooltips //do not remove/change this line

 tooltips[5654]=["/mapimages/", "<br><br>Objectives:<br>A: <br>B: <br>C: <br>D: <br><br>Terrain: <br><br>Creator: <br><br>Modder: <br><br>Mods: ", {background:"gray", border:"none", font:"bold 12px Arial", color:"black"}]
}(),
tooltipoffsets: [20, -30], //additional x and y offset from mouse cursor for tooltips
//***** NO NEED TO EDIT BEYOND HERE

 

what do I need to do to place it at the right spot?

Edited by dodgeitorelse3
Link to comment
Share on other sites

Do you have to use fwrite?

 

Another way you could do it is by reading the file contents into an array using file().

Then use preg_grep to match the line return tooltips (this will return an array with the data as a value, and the key is the index in the array that the data is - so basically the line number in the file).

 

Then you grab the array key, using a simple foreach loop

$file = file("test_file.text");
$fileArray = preg_grep("/^return tooltips$/", $file);
$line = 0; //This will store the line number of 'return tooltips'
foreach($fileArray as $index => $data){ //This will most likely only run once, but that's fine, we're only grabbing the array key
 $line = $index-1; //Store the index number just before the the line you searched for
}

Then you use array_splice to put data into the array at position $line. You may need to use a loop and increase $line each time you add to the array, so it adds it _after_ each previous line that you add, otherwise it will print backwards (last line will appear first, first line will appear last)

 

Then you can use file_put_contents("new_file.txt") to put the array back into a file.

 

Hopefully that's understandable.

 

Denno

Link to comment
Share on other sites

//loop through array using foreach
foreach($lines as $line)
{
        fwrite($f,$line); //place $line back in file
if (strstr($line,$key)){ //look for $key in each line
fwrite($f,$new_tt);
} //place $line back in file

 

Look a little closer. You are writing the line (the one that contains the key) before you check to see if the line is the key. The IF test, and writing the new line should occur before you write the current line (the one with the key).

Link to comment
Share on other sites

@ DavidAM,

I tried your suggestion first by changing code to

 

//loop through array using foreach
foreach($lines as $line)
{
   if (strstr($line,$key)){ //look for $key in each line
   fwrite($f,$new_tt);
   fwrite($f,$line); //place $line back in file

   }
}

 

which did put the new line before the key line but it did not put the first part of the file back in giving me this

 


    tooltips[5654]=["/mapimages/", "<br><br>Objectives:<br>A: <br>B: <br>C: <br>D: <br><br>Terrain: <br><br>Creator: <br><br>Modder: <br><br>Mods: ", {background:"gray", border:"none", font:"bold 12px Arial", color:"black"}]
 return tooltips //do not remove/change this line

as my finished file. I assume I misunderstood what you said.

 

 

@ denno020,

I have to look at the preg_grep, the array_splice and the file_put_contents as all of this file writing stuff is new to me.

Link to comment
Share on other sites

ok, after fiddling around with this I used

//loop through array using foreach
foreach($lines as $line)
{
 if (strstr($line,$key)){ //look for $key in each line
fwrite($f,$new_tt);
}
fwrite($f,$line); //place $line back in file
}

 

and it works like a charm.

 

davidAM thank you very much.

 

@ denno020, I will still try what you suggested so I can learn a bit more so a thank you to you also.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.