Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/forums/php/)
-   -   Fseek Function not working !!!! (http://www.go4expert.com/forums/fseek-function-t16242/)

Bhullarz 18Feb2009 10:03

Fseek Function not working !!!!
 
Code:

<?php
$ourFileName="newfile.txt";
$fh = fopen($ourFileName, 'a+') or die("Can't open file");
echo " File opened: ".ftell($fh);
fwrite($fh,"<image></image>");
fseek($fh,-8,SEEK_END);
fwrite($fh,"Data Written");
fclose($fh);
?>

Whats wrong here?
Its output is as follows:
Code:

<image></image>Data Written
Where as expected output is:
Code:

<image>Data Written</image>
Any Suggestions?

shabbir 18Feb2009 10:10

Re: Fseek Function not working !!!!
 
Just try the following.

Close the file after writing string and then open back to write and see what is the output?

Bhullarz 18Feb2009 11:02

Re: Fseek Function not working !!!!
 
Quote:

Originally Posted by shabbir (Post 43055)
Just try the following.

Close the file after writing string and then open back to write and see what is the output?

Even after closing the file, output is same. I just forgot to put fclose here. In file, I am using that one. I have edited the post.

More over, whenever I re-run the PHP page, it should return the current pointer position which should be the at the end here. But ftell function returns 0 everytime, whether text file has data or not.

shabbir 18Feb2009 12:27

Re: Fseek Function not working !!!!
 
When you open the file in append mode it always writes to the end of the file and not where the FILE_POINTER

Bhullarz 18Feb2009 12:35

Re: Fseek Function not working !!!!
 
Quote:

Originally Posted by shabbir (Post 43074)
When you open the file in append mode it always writes to the end of the file and not where the FILE_POINTER

Sir if so , why ftell() function is always resulting 0 whenever it is re-opened. It should give the last point from where it has to start writing. Moreover I am using fseek() function to move the pointer to -8 from end-of-file.

shabbir 18Feb2009 13:11

Re: Fseek Function not working !!!!
 
Probably what I can get is the file is opened and 0 is set at the last or something like that so that you can not do anything to the previous data.

Bhullarz 18Feb2009 15:26

Re: Fseek Function not working !!!!
 
Quote:

Originally Posted by shabbir (Post 43080)
Probably what I can get is the file is opened and 0 is set at the last or something like that so that you can not do anything to the previous data.

In this case, how can we edit a text file instead of just writing the data at the end of the file?

shabbir 18Feb2009 15:47

Re: Fseek Function not working !!!!
 
Probably r+

Bhullarz 18Feb2009 22:00

Re: Fseek Function not working !!!!
 
Quote:

Originally Posted by shabbir (Post 43097)
Probably r+

Ok. I used r+ and added some more logic to it.
Code:

<?php

$ourFileName="newfile.txt";

if(is_file($ourFileName))
        $fh = fopen($ourFileName, 'r+') or die("Can't open file");
else
        $fh = fopen($ourFileName, 'w') or die("Can't open file");

fwrite($fh,"<image></image>");
fseek($fh,-8,SEEK_END);
fwrite($fh,"Data Written");
fclose($fh);

?>

But still I am not getting expected result.
now the result is as follows:
Code:

<image>Data Written
[/code]

shabbir 19Feb2009 09:17

Re: Fseek Function not working !!!!
 
PHP does not have a function to insert text into the middle of a file and you have to read the complete data and then insert the data in the variables and write it back. Thats the best way to go about getting this done.


All times are GMT +5.5. The time now is 22:00.