Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 805
  • Last Modified:

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

0
duncanb7
Asked:
duncanb7
  • 4
  • 3
4 Solutions
 
nepaluzCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nepaluzCommented:
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
 
nepaluzCommented:
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 PaseurCommented:
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now