ID: 49988 Updated by: [email protected] Reported By: mwease at tx dot rr dot com -Status: Open +Status: Bogus -Bug Type: Unknown/Other Function +Bug Type: Strings related Operating System: Linux 2.0 PHP Version: 5.3.0 New Comment:
1. You really need to include the \n in the format string.. 2. What do _you_ expect the first %s in the reading to match? Now think what the computer does.. :) (hint: it's all string to it..) You need to use more precise format. Simple %s just won't work. http://www.php.net/manual/en/function.sprintf.php and example 6.. Basically you're writing/reading CSV format here, why not use those funcs instead? There is no bug here. Previous Comments: ------------------------------------------------------------------------ [2009-10-25 01:41:24] mwease at tx dot rr dot com 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 this bug report at http://bugs.php.net/?id=49988&edit=1
