Infinite Loop Problems

I have a while loop:

[php]
while ( ($inchar = fgetc($fp) != ‘’’) {
$quote = $quote . $inchar;
}
[/php]

And this is an infinite loop I have determined. I have written this same exact loop in C before with no problem, so I thought I would try it here.

I also tried changing the syntax to this

[php]
$inchar = fgetc($fp);
while ($inchar != ‘’’) {
$quote = $quote . $inchar;
$inchar = fgetc($fp);
}
[/php]

Can somebody help me resolve why this loop is occuring. I should go about three characters in the file I am reading from before is get to the escape.

Thanks

$inchar = fgetc($fp) != ‘’’ <- your using a single quote and then a double quote on the back slash

That is a single quote the a backslash the a single quote then a single quote.

I am using an excape character for the single quote.

When using SINGLE QUOTES to define your text (in this case a character) You do NOT need to escape it. THe only real restriction is that you cannot use a single quote as part of your string.

So in your instance, since you are doing a comparison the operation of $inchar != ‘’’ will never be true.

Example

$inchar contains the but you are comparing it against
Reality… A single character will NEVER be equal to a ’ (backslash and single quote) or in this case will ALWAYS be NOT EQUAL to a ’ (backslash and single quote) .

I think what would be more appropriate would be if you are trying to single out a SINGLE QUOTE then use

[php]
// NOT EQUAL TO a SINGLE QUOTE (syntax is a DOUBLE Quote SINGLE Quote DOUBLE Quote)
while ( ($inchar = fgetc($fp) != “’”) {
$quote = $quote . $inchar;
}
?>
[/php]

Hope this helps

Actually, due to the operator priority, the result of the comparision will probably be stored in the comparision, which is probably no what you want. You should also check for the end of file while looping.

while( !feof( $fp ) && ( $inchar = fgetc($fp) ) != "/" )
    $quote .= $inchar;

Note that I changed your concatenation for the .- operator. It will append the character to the string instead of allocating an entire new buffer (which will be MUCH faster).

That seemed to do it. Thank you

I have another question.

I am using the strpos() function to search for a string in a file, but it does not move the character the file is pointing at. What I want is to find a string x the read some with fgetc() from that new point then search again starting at that new point down the file fgetc() again and so on.

I have not been able to find this function and have looked at a few web sites. Can you help?

Dustin

So what are you could do using a couple of functions in combination.

some kind of loop until the end of the string:
    strpos (find first occurance of a case-sensitive string - can use 3rd param to tell where to start again)
    substr (return a substring of the string to work on - start position is returned by strpos)
    do whatever with your substring
end loop using last position returned by strpos as the starting position
Sponsor our Newsletter | Privacy Policy | Terms of Service