|
Posted by Armando Padilla on 08/11/06 03:01
fool wrote:
> Dear group,
> I am a beginner in php and I was little bit experience in C language.
> I
> want to read a file's content and delete the first line of the file and
>
> display those lines which has got deleted.
>
> The content of the external file , which is in .cvs format is:
>
> -------------------------------------------------------------------
> Agency,Ebook,Date,Sold#,Sold$,Refund#,Refund$,GAW#,GAW$,Income
> cbezy,Dog,8/7/06,0,0,0,0,204,16.88,-16.88
> cbezy,CMS,8/7/06,1,10.79,0,0,0,0,10.79
> cbgeo,Sudoku,8/7/06,1,14.72,0,0,0,0,14.72
> cbgeo,Cock,8/7/06,1,14.72,0,0,0,0,14.72
> ppgeo,Sudoku,8/7/06,17,229.58,2,24.16,2286,118.62,86.8
> ppgeo,Pythons,8/7/06,1,19.36,0,0,250,8.84,10.52
> ppgeo,Cockatiels,8/7/06,9,108.46,1,16.21,0,0,92.25
> ppgeo,Cockatoos,8/7/06,5,72.66,0,0,743,47.56,25.1
> ppgeo,Wastewater,8/7/06,1,44.87,0,0,49,5.25,39.62
> ppgeo,Chinchillas,8/7/06,1,6.91,0,0,0,0,6.91
> ppgeo,Modhomes,8/7/06,0,0,0,0,189,99.94,-99.94
> ppgeo,Smormonsub,8/7/06,1,3.54,0,0,0,0,3.54
> --------------------------------------------------------------------
>
> The small program which I wrote is
>
> <?php
> function read_file($fname)
> {
> $f = fopen($fname,"r");
> if(!$f)
> die("file does not exist");
> $buff = fread($f,filesize($fname));
> if(!$buff)
> die("file reading failed");
> return $buff;
> }
>
> function parse_condent($buff,$fname)
> {
> $f = fopen($fname, "a+");
> if(!$f)
> die("file does not exist");
> if($buff == '\n' || $buff == '\r' || $buff == '\t') /*####*/
> unset($buff);
>
> return $buff;
> }
> $f = read_file("1.csv");
>
> echo $f.'<br> before delition <br>';
>
> $p = parse_condent($f,"1.csv");
>
> echo $p.'<br> After deletion';
> ?>
>
> when the line marked with /*####*/ is taken then the whole array is
> getting
> deleted. I want the specified line and chars. Any help or url link is
> appreciated.
>
> <OT>
> In C programming the string, which is a pointer is iterated and
> checked with
> a specified pattern. How is that possible in php?
> </OT>
>
Hi there,
humm so you want the first line back right? The first line is the one
you deleted? If thats the case below is the modifucations to your code.
Also buff gets reset once it hits unset() so it returns nothing.
<?php
function read_file($fname)
{
$f = fopen($fname,"r");
if(!$f)
die("file does not exist");
$buff = fread($f,filesize($fname));
if(!$buff)
die("file reading failed");
return $buff;
}
function parse_condent($buff,$fname)
{
$f = fopen($fname, "a+");
if(!$f)
die("file does not exist");
$content = fread($f, 20000);
$lines = explode("\n", $content);
//remoevs the first line
$lines = array_splice($lines, 0, -1);
return $lines[0];
}
$f = read_file("test.csv");
echo $f.'<br> before delition <br><br>';
$p = parse_condent($f,"test.csv");
echo $p.'<br> After deletion';
?>
Navigation:
[Reply to this message]
|