strcmp problem

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

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

name: Juicy
age: 25
user text here

And I read the text file  

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

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

  if(strcmp($content, "#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.


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
     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;

Site Timeline