strcmp problem

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

    strcmp problem

    I done a program to read text file, where content is

    #Header#
    name: Juicy
    age: 25
    @Header@
    #Text#
    user text here
    @Text@
    #End#
    tel
    fax
    @End@
    eof

    And I read the text file

    $filename = "/usr/local/file/rpt.txt";
    $fp = fopen($filename , 'r');

    $content = fgets($fp);
    while(strcmp($c ontent,"@End@") <>0) {

    $content = fgets($fp);

    if(strcmp($cont ent, "#Text#")== 0){ //not match
    $strPretext .= $content;
    }//end if
    //end while


    I should get the data from #Text# until one line before @End@, but I get
    nothing, I found that the strcmp for #Text# always not match when it read
    through whole file.


    I cant find out what goes wrong. Please advise.

    Thanks.


  • Rex

    #2
    Re: strcmp problem

    Some observations: When reading a file you can use the funciton
    feof() to check if a file pointer has reached the end of file marker.
    This is useful when looping thru a file's contents.
    I dont believe the inital fgets is needed -- just grab the first
    line in the loop.
    Also, instead of using the binary string comparision function use
    the == operator and trim $contents to remove any uneeded whitespace
    chars.
    I'm not sure if the following listing is what you're looking for
    as $strPretext only contains "#Text#" once the code is executed but i
    think it's a step in the right direction.
    If you're looking for the line after the "#Text#" line just do
    another $content = fgets($fp); (line read) inside the if stmnt.



    $filename = "/usr/local/file/rpt.txt";
    $fp = fopen($filename , 'r');
    $strPretext = "";

    while( !feof($fp) ) {
    $content = fgets($fp);
    if( trim($content) == "#Text#") {
    $strPretext .= $content;
    }
    }

    echo $strPretext;

    Comment

    Working...