File reading and deleting a pattern (beginner)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fool

    File reading and deleting a pattern (beginner)

    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,Da te,Sold#,Sold$, Refund#,Refund$ ,GAW#,GAW$,Inco me
    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,Cockatiel s,8/7/06,9,108.46,1,1 6.21,0,0,92.25
    ppgeo,Cockatoos ,8/7/06,5,72.66,0,0, 743,47.56,25.1
    ppgeo,Wastewate r,8/7/06,1,44.87,0,0, 49,5.25,39.62
    ppgeo,Chinchill as,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,Smormonsu b,8/7/06,1,3.54,0,0,0 ,0,3.54
    --------------------------------------------------------------------

    The small program which I wrote is

    <?php
    function read_file($fnam e)
    {
    $f = fopen($fname,"r ");
    if(!$f)
    die("file does not exist");
    $buff = fread($f,filesi ze($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.cs v");

    echo $f.'<brbefore delition <br>';

    $p = parse_condent($ f,"1.csv");

    echo $p.'<brAfter 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>

  • Armando Padilla

    #2
    Re: File reading and deleting a pattern (beginner)

    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,Da te,Sold#,Sold$, Refund#,Refund$ ,GAW#,GAW$,Inco me
    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,Cockatiel s,8/7/06,9,108.46,1,1 6.21,0,0,92.25
    ppgeo,Cockatoos ,8/7/06,5,72.66,0,0, 743,47.56,25.1
    ppgeo,Wastewate r,8/7/06,1,44.87,0,0, 49,5.25,39.62
    ppgeo,Chinchill as,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,Smormonsu b,8/7/06,1,3.54,0,0,0 ,0,3.54
    --------------------------------------------------------------------
    >
    The small program which I wrote is
    >
    <?php
    function read_file($fnam e)
    {
    $f = fopen($fname,"r ");
    if(!$f)
    die("file does not exist");
    $buff = fread($f,filesi ze($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.cs v");
    >
    echo $f.'<brbefore delition <br>';
    >
    $p = parse_condent($ f,"1.csv");
    >
    echo $p.'<brAfter 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($fnam e)
    {
    $f = fopen($fname,"r ");
    if(!$f)
    die("file does not exist");
    $buff = fread($f,filesi ze($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($l ines, 0, -1);



    return $lines[0];
    }
    $f = read_file("test .csv");

    echo $f.'<brbefore delition <br><br>';

    $p = parse_condent($ f,"test.csv") ;

    echo $p.'<brAfter deletion';
    ?>

    Comment

    • fool

      #3
      Re: File reading and deleting a pattern (beginner)

      In article <9qSCg.7656$FN2 .3232@newssvr14 .news.prodigy.c om>,
      mando81@prodigy .net says...
      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,Da te,Sold#,Sold$, Refund#,Refund$ ,GAW#,GAW$,Inco me
      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,Cockatiel s,8/7/06,9,108.46,1,1 6.21,0,0,92.25
      ppgeo,Cockatoos ,8/7/06,5,72.66,0,0, 743,47.56,25.1
      ppgeo,Wastewate r,8/7/06,1,44.87,0,0, 49,5.25,39.62
      ppgeo,Chinchill as,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,Smormonsu b,8/7/06,1,3.54,0,0,0 ,0,3.54
      --------------------------------------------------------------------

      The small program which I wrote is

      <?php
      function read_file($fnam e)
      {
      $f = fopen($fname,"r ");
      if(!$f)
      die("file does not exist");
      $buff = fread($f,filesi ze($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;
      }
      Thanks for the code. I have learnt somthing.

      Comment

      Working...