From:             mwease at tx dot rr dot com
Operating system: Linux 2.0
PHP version:      5.3.0
PHP Bug Type:     Unknown/Other Function
Bug description:  fscanf() function doesn't work as specified

Description:
------------
Maybe I'm doing something wrong, but I don't think so. 

The documentation for fprintf() and fscanf() say that those functions
use the format string as defined in the documentation for sprintf().
This is not so.  fprintf() works that way, but not fscanf().  One
should actually be REQUIRED to use exactly the same format for each
if they expect what is written with fprintf() to be read by fscanf(),
but this does not work.

Reproduce code:
---------------
fprintf($entryfile,
  "%s~%s~%s~%s~%s~%s~%s~%s~%s~%s~%s~%s~%s~%s~%s~%s~%s~\n",$entrynum",
  $timestamp,$tripmiles,$loadingtype,$content,$origcity,
  $origstate,$firstdate,$lastdate,$destcity,$deststate,
  $trucktypes,$entrynotes,$thisusername,$contact,
  $_SESSION["company_name"],$phone);

output in file is:

2~200908301103~386~Drop~Unknown~Aliceville~AL~9/30/09~10/2/09~Pembroke~GA~Hopper~~JimmyB~Kyle~Ampro~800-849-6647~

$numfields = 
  fscanf($entryfile,
  "%s~%s~%s~%s~%s~%s~%s~%s~%s~%s~%s~%s~%s~%s~%s~%s~%s~",
  $fentrynum,$fdatetime,$ftripmiles,$floadingtype,$fcontent,
  $forigcity,$forigstate,$ffirstdate,$flastdate,$fdestcity,
  $fdeststate,$ftrucktypes,$fentrynotes,$fusername,$fcontact,
  $fcompany,$fphone)

Expected result:
----------------
fprintf() writes exactly the output I expect, i.e., the values of
the variables listed, all separated by the tilde (~).  some of the
variables are multiple words and one of them can be 80 chars long and
at least one can be null, resulting in two tildes one after another
'~~'.  fprintf() handles it.

since i used the exact same format, minus the "\n" at the end,
fscanf() should return exactly what fprintf() wrote, assigning the
same variables to exactly what they were when written with fprintf().

Actual result:
--------------
this is what and how fscanf() reads the first line (these are results of
echo statements):

numfields=1;
fentrynum=2~200908301103~386~Drop~Unknown~Aliceville~AL~9/30/09~10/2/09~Pembroke~GA~Hopper~~JimmyB~Kyle~Ampro~800-849-6647~

only the first variable was assigned and to the entire line from the
file, as evidenced by both echoes.  all other variables are null.  if
i don't include the variables on the fscanf(), only the first element
of the resulting array is assigned.

apparently, the fscanf() also skipped every other line in the file.
this is the second line printed, but is actually the third line of
the file written with fprintf():

numfields=1;
fentrynum=4~200908301103~642~Drop~Unknown~Birmingham~AL~9/30/09~10/1/09~Pompano

the scanning of the input line stopped at the blank in "pompano 
beach", which is the first blank in the data.  This is also wrong and not
what fprintf() does with the same
format string.

i cannot place blanks to match those in my data because i have no way
of knowing where they will be and i shouldn't have to.

-- 
Edit bug report at http://bugs.php.net/?id=49988&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=49988&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=49988&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=49988&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=49988&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=49988&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=49988&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=49988&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=49988&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=49988&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=49988&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=49988&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=49988&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=49988&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=49988&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=49988&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=49988&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=49988&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=49988&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=49988&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=49988&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=49988&r=mysqlcfg

Reply via email to