Re: A problem with fgets()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, May 30, 2008 at 4:21 AM, Chris <dmagick@xxxxxxxxx> wrote:
>
>
>> I just need to figure out why when using fgets() with fseek() &
>> rand(), the script returns partial strings form the city names.
>
> Because fseek doesn't necessarily put you at the start of a line.
>
> It puts you anywhere (which could be the start, middle, 3 chars from the
> end) according to the number of bytes you tell it to start at.
>
> Then fgets reads the rest of the line.
>
> A file that looks like this:
>
> (numbers are the "bytes" for ease of explanation)
>
> 123456789
>
> is going to be different to a file that looks like this:
>
> 1234
> 56
> 789
>
> and if you tell me you want to start at "byte 5", then in file 1, that's
> the middle - in file 2 that's the start of the second line (#4 is a
> newline char :P).
>

So you're confirming that fgets() doesn't necessarily read a whole
line? This user note existed on the manual's page of fgets() since
2004 and nobody deleted it or commented about:

rstefanowski at wi dot ps dot pl
12-Aug-2004 09:03

"Take note that fgets() reads 'whole lines'. This means that if a file
pointer is in the middle of the line (eg. after fscanf()), fgets()
will read the following line, not the remaining part of the currnet
line. You could expect it would read until the end of the current
line, but it doesn't. It skips to the next full line."

That was my source of confusion.

Regards,
Usamah

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux