fseek() is not working in my PHP5.2.16 Linux

Dear Experts,
I get one simple example csv file such as
1,2,3  
4,5,6
7,8,9
Total 10 bytes for each row
And I write php file and if every line's first cell is > 0 then overwrite the same cell value by 0 , So
if php pass, the output should like this
0,2,3
0,5,6
0,8,9

So I used fopen with "r+" option because read and write within the same file
and use fseek() with negative offset to backward file pointer to the previous row
since every row is just 10 byte and I used -10 in fseek() and Ftell() is also report correct file pointer
But the question fseek()  could not  locate the exact file pointer by the system with the
Warning: fseek() [function.fseek]: stream does not support seeking in www.mydomain.data.php on line

So the output is same as before, so fseek is fail in my php data.php and I also try
rewind() that is also NO any response.

SO anyone know what is the warning meaning ? and I check a lot thing at
http://hk.php.net/manual/en/book.stream.php and
see the stream data method at  http://hk.php.net/manual/en/book.stream.php

write a simple data into csv file why it is so difficult. Please advise.

Duncan

/*******data.php****?

<?php
/*phpinfo();*/
$file_handle = fopen("javascript.csv", "r+");
while (!feof($file_handle) ) {
echo ftell($file_handle);
echo '<br></br>';
$line_of_text = fgetcsv($file_handle, 10);
echo '<br></br>';

if ($line_of_text[0] >0 ) {
echo $line_of_text[0].'<br></br>' ;
fseek($file_handle, -10, SEEK_SET);
$line_of_text[0] =0;
fwrite($file_handle, $line_of_text);
   }
}

Open in new window

LVL 13
duncanb7Asked:
Who is Participating?
 
nepaluzConnect With a Mentor Commented:
use this, I just gave you a response on your other question:

$file = "javascript.csv";
$proverbs = file($file);
$num = count($proverbs);
for ($c=0; $c < $num; ++$c) {
	$custom =  array_shift($proverbs);
	$line_of_text = explode(',', $custom);
	if ($line_of_text[0] > 0) {
                   echo $line_of_text[0].'<br></br>' ;
                   $line_of_text[0] =1232222;
	   $proverbs[] = implode(",",$line_of_text);
	}
}
file_put_contents($file, $proverbs); 

Open in new window

0
 
duncanb7Author Commented:
Thanks , I will look into it and try
0
 
duncanb7Author Commented:
it work for fine if we use file() and array_shift instead of fseek() but this thread is more
concentration on fseek() to achieve the goal if we could use it.
 
Or could we tell when and why we use fseek() and arry_shift() ?

Please advise

Dunca
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
nepaluzConnect With a Mentor Commented:
you will have to change the code and put the implde line OUTSIDE the conditional test for the first item in the line.

$file = "javascript.csv";
$proverbs = file($file);
$num = count($proverbs);
for ($c=0; $c < $num; ++$c) {
	$custom =  array_shift($proverbs);
	$line_of_text = explode(',', $custom);
	if ($line_of_text[0] > 0) {
                   echo $line_of_text[0].'<br></br>' ;
                   $line_of_text[0] =1232222;
	}
	$proverbs[] = implode(",",$line_of_text);
}
file_put_contents($file, $proverbs);

Open in new window

0
 
nepaluzConnect With a Mentor Commented:
I wrote before reading your response.

The question was posted in PHP and the answer provided is also PHP. If you want a solution to fseek(), someone  esle will probably have to step in.

The suggested code, as you say, solves your problem, and I have further refined / corrected it for you as there was a flaw (the resutant file would have contained ONLY the amended lines and NO others)

0
 
duncanb7Author Commented:
Thanks for your start and  get more interested in php.

If others experts know more fseek() usage, please advise

Duncan
0
 
Ray PaseurConnect With a Mentor Commented:
It's always a good idea to read the online man pages and especially the user-contributed notes.  Lots of experts contribute to the PHP web site:
http://us3.php.net/manual/en/function.fseek.php#102994
0
 
duncanb7Author Commented:
Thanks for all of your reply
It seems no one interested in
fseek()
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.