Problem wwith strcmp() function

This is a discussion on Problem wwith strcmp() function within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Dear group. Can someone please tell me why the following program is not finding a match? Is something wrong in ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-26-2006
Henk Oegema
 
Posts: n/a
Default Problem wwith strcmp() function

Dear group.

Can someone please tell me why the following program is not finding a match?
Is something wrong in the strcmp() function ?
When I leave out the strcmp() and only take the while loop with the print
command inside, it prints all the 10 lines from the file tekst.

=========================
<?php

$filename=tekst;
$targetline="line 5";


$fd = fopen($filename, "r");

while(!feof($fd))
{
$line = fgets($fd,80);
if(strcmp($line, $targetline) == 0)
{
echo "A match was found!";
print("$line <br>");
}
}

fclose($fd);

?>
=========================

File tekst:
=======
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
=======

Rgds,
Henk Oegema

Reply With Quote
  #2 (permalink)  
Old 06-26-2006
David Haynes
 
Posts: n/a
Default Re: Problem wwith strcmp() function

Henk Oegema wrote:
> Dear group.
>
> Can someone please tell me why the following program is not finding a match?
> Is something wrong in the strcmp() function ?
> When I leave out the strcmp() and only take the while loop with the print
> command inside, it prints all the 10 lines from the file tekst.
>
> =========================
> <?php
>
> $filename=tekst;
> $targetline="line 5";
>
>
> $fd = fopen($filename, "r");
>
> while(!feof($fd))
> {
> $line = fgets($fd,80);
> if(strcmp($line, $targetline) == 0)
> {
> echo "A match was found!";
> print("$line <br>");
> }
> }
>
> fclose($fd);
>
> ?>
> =========================
>
> File tekst:
> =======
> line 1
> line 2
> line 3
> line 4
> line 5
> line 6
> line 7
> line 8
> line 9
> line 10
> =======
>
> Rgds,
> Henk Oegema
>


Caveat: I have not tested this

I think the problem you are having is that fgets returns the entire line
including the carriage-return. So, 'line 5' does not equal 'line 5\r\n'.
Try doing the following:

if( strncmp($line, $targetline, strlen($targetline)) {

and see if that helps.

-david-

Reply With Quote
  #3 (permalink)  
Old 06-29-2006
Henk Oegema
 
Posts: n/a
Default Re: Problem wwith strcmp() function

David Haynes wrote:

> Henk Oegema wrote:
>> Dear group.
>>
>> Can someone please tell me why the following program is not finding a
>> match? Is something wrong in the strcmp() function ?
>> When I leave out the strcmp() and only take the while loop with the print
>> command inside, it prints all the 10 lines from the file tekst.
>>
>> =========================
>> <?php
>>
>> $filename=tekst;
>> $targetline="line 5";
>>
>>
>> $fd = fopen($filename, "r");
>>
>> while(!feof($fd))
>> {
>> $line = fgets($fd,80);
>> if(strcmp($line, $targetline) == 0)
>> {
>> echo "A match was found!";
>> print("$line <br>");
>> }
>> }
>>
>> fclose($fd);
>>
>> ?>
>> =========================
>>
>> File tekst:
>> =======
>> line 1
>> line 2
>> line 3
>> line 4
>> line 5
>> line 6
>> line 7
>> line 8
>> line 9
>> line 10
>> =======
>>
>> Rgds,
>> Henk Oegema
>>

>
> Caveat: I have not tested this
>
> I think the problem you are having is that fgets returns the entire line
> including the carriage-return. So, 'line 5' does not equal 'line 5\r\n'.
> Try doing the following:
>
> if( strncmp($line, $targetline, strlen($targetline)) {
>
> and see if that helps.
>
> -david-


After a lot of trying I have not been able to get it working yet.

I changed $targetline="line 5" to $targetline="line 5\r\n";
I also tried to change
if(strcmp($line, $targetline) == 0)
to
if(strcmp($line, $targetline, strenlen($targetline)) == 0)

Henk

Reply With Quote
  #4 (permalink)  
Old 06-29-2006
David Haynes
 
Posts: n/a
Default Re: Problem wwith strcmp() function

Henk Oegema wrote:
> David Haynes wrote:
>
>> Henk Oegema wrote:
>>> Dear group.
>>>
>>> Can someone please tell me why the following program is not finding a
>>> match? Is something wrong in the strcmp() function ?
>>> When I leave out the strcmp() and only take the while loop with the print
>>> command inside, it prints all the 10 lines from the file tekst.
>>>
>>> =========================
>>> <?php
>>>
>>> $filename=tekst;
>>> $targetline="line 5";
>>>
>>>
>>> $fd = fopen($filename, "r");
>>>
>>> while(!feof($fd))
>>> {
>>> $line = fgets($fd,80);
>>> if(strcmp($line, $targetline) == 0)
>>> {
>>> echo "A match was found!";
>>> print("$line <br>");
>>> }
>>> }
>>>
>>> fclose($fd);
>>>
>>> ?>
>>> =========================
>>>
>>> File tekst:
>>> =======
>>> line 1
>>> line 2
>>> line 3
>>> line 4
>>> line 5
>>> line 6
>>> line 7
>>> line 8
>>> line 9
>>> line 10
>>> =======
>>>
>>> Rgds,
>>> Henk Oegema
>>>

>> Caveat: I have not tested this
>>
>> I think the problem you are having is that fgets returns the entire line
>> including the carriage-return. So, 'line 5' does not equal 'line 5\r\n'.
>> Try doing the following:
>>
>> if( strncmp($line, $targetline, strlen($targetline)) {
>>
>> and see if that helps.
>>
>> -david-

>
> After a lot of trying I have not been able to get it working yet.
>
> I changed $targetline="line 5" to $targetline="line 5\r\n";
> I also tried to change
> if(strcmp($line, $targetline) == 0)
> to
> if(strcmp($line, $targetline, strenlen($targetline)) == 0)
>
> Henk
>


Henk:
This code works on my system:
<?php
$filename='tekst';
$targetline='line 5';

$fd = fopen($filename, 'r');
while(!feof($fd)) {
$line = fgets($fd, 80);
//echo "line = $line";
if(strncmp($line, $targetline, strlen($targetline)) == 0) {
echo "A match was found! ";
print("$line <br>");
}
}
fclose($fd);
?>

Also, the line ending will be either '\n' for Linux/Unix or '\r\n' for
Windows. That is why using strncmp is a better choice.

-david-

Reply With Quote
  #5 (permalink)  
Old 06-30-2006
Henk Oegema
 
Posts: n/a
Default Re: Problem wwith strcmp() function

David Haynes wrote:

> Henk Oegema wrote:
>> David Haynes wrote:
>>
>>> Henk Oegema wrote:
>>>> Dear group.
>>>>
>>>> Can someone please tell me why the following program is not finding a
>>>> match? Is something wrong in the strcmp() function ?
>>>> When I leave out the strcmp() and only take the while loop with the
>>>> print
>>>> command inside, it prints all the 10 lines from the file tekst.
>>>>
>>>> =========================
>>>> <?php
>>>>
>>>> $filename=tekst;
>>>> $targetline="line 5";
>>>>
>>>>
>>>> $fd = fopen($filename, "r");
>>>>
>>>> while(!feof($fd))
>>>> {
>>>> $line = fgets($fd,80);
>>>> if(strcmp($line, $targetline) == 0)
>>>> {
>>>> echo "A match was found!";
>>>> print("$line <br>");
>>>> }
>>>> }
>>>>
>>>> fclose($fd);
>>>>
>>>> ?>
>>>> =========================
>>>>
>>>> File tekst:
>>>> =======
>>>> line 1
>>>> line 2
>>>> line 3
>>>> line 4
>>>> line 5
>>>> line 6
>>>> line 7
>>>> line 8
>>>> line 9
>>>> line 10
>>>> =======
>>>>
>>>> Rgds,
>>>> Henk Oegema
>>>>
>>> Caveat: I have not tested this
>>>
>>> I think the problem you are having is that fgets returns the entire line
>>> including the carriage-return. So, 'line 5' does not equal 'line 5\r\n'.
>>> Try doing the following:
>>>
>>> if( strncmp($line, $targetline, strlen($targetline)) {
>>>
>>> and see if that helps.
>>>
>>> -david-

>>
>> After a lot of trying I have not been able to get it working yet.
>>
>> I changed $targetline="line 5" to $targetline="line 5\r\n";
>> I also tried to change
>> if(strcmp($line, $targetline) == 0)
>> to
>> if(strcmp($line, $targetline, strenlen($targetline)) == 0)
>>
>> Henk
>>

>
> Henk:
> This code works on my system:
> <?php
> $filename='tekst';
> $targetline='line 5';
>
> $fd = fopen($filename, 'r');
> while(!feof($fd)) {
> $line = fgets($fd, 80);
> //echo "line = $line";
> if(strncmp($line, $targetline, strlen($targetline)) == 0) {
> echo "A match was found! ";
> print("$line <br>");
> }
> }
> fclose($fd);
> ?>
>
> Also, the line ending will be either '\n' for Linux/Unix or '\r\n' for
> Windows. That is why using strncmp is a better choice.
>
> -david-


YES David, when I changed $targetline="line 5\r\n" to $targetline="line 5\n"
it worked for me too.
Thanks very much.

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:52 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0