[PHP] fscanf and memory
Hi, I don't know if it's a bug in php but if I do $fp=fopen("some_file","r"); while (!feof($fp)) { fscanf($fp, "%d %d",&$var1,&$var2); // some treatment } fclose($fp); and the file is big enough, php eat up memory like hot cakes. But if I do $fp=fopen("some_file","r"); while (!feof($fp)) { $line = fscanf($fp, "%d %d"); list ($var1, $var2) = $line; // some treatment } fclose($fp); It work just fine. Seems a problem with fscanf. Has someone ever experienced this? Any advises? Oh btw I'm using php 4.3.4 with Apache 1.3.29 Thanks in advance Sébastien Guay _ MSN Search, le moteur de recherche qui pense comme vous ! http://fr.ca.search.msn.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] fscanf and memory
Hi, Friday, February 27, 2004, 4:48:20 AM, you wrote: SG> Hi, SG> I don't know if it's a bug in php but if I do SG> $fp=fopen("some_file","r"); SG> while (!feof($fp)) SG> { SG> fscanf($fp, "%d %d",&$var1,&$var2); SG> // some treatment SG> } SG> fclose($fp); SG> and the file is big enough, php eat up memory like hot cakes. SG> But if I do SG> $fp=fopen("some_file","r"); SG> while (!feof($fp)) SG> { SG> $line = fscanf($fp, "%d %d"); SG> list ($var1, $var2) = $line; SG> // some treatment SG> } SG> fclose($fp); SG> It work just fine. Seems a problem with fscanf. SG> Has someone ever experienced this? Any advises? SG> Oh btw I'm using php 4.3.4 with Apache 1.3.29 SG> Thanks in advance SG> Sébastien Guay SG> _ SG> MSN Search, le moteur de recherche qui pense comme vous ! SG> http://fr.ca.search.msn.com/ Thats not the way to use scanf, I think you are doing a scanf for each line in the file. It should be something like: while(fscanf($fp, "%d %d",&$var1,&$var2)){ //... do something with the values } -- regards, Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] fscanf
Hi I'm having dramers with fscanf I have a line: text, text, text I can't work out how to scan removing the comma and whitespace. the text can be absolutle anything. at the moment I have $array = fscanf ($fp, %s%[^,] %s%[^,] %s%[^,]); Thanks Kris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] fscanf - string format -> clear discription
where can i find a discription of the possible options for the "sting format" in the fscanf command [ fscanf ( int handle, string format [, string var1]) ] For example, what means: "%s\t or %s\n in the command fscanf ($fp, "%s\t%s\t%s\n"); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] fscanf Help!!
I'm starting to learn the language and I'm having a problem with fscanf as witnessed below: http://www.zephyr-works.com/ascii-5/dev/phpnews.phps The output is at: http://www.zephyr-works.com/ascii-5/dev/phpnews.php I can't seem to get the variables to assign correctly... The source file is: http://www.zephyr-works.com/ascii-5/dev/slashdot.xml Thanks!! James _ Get your FREE download of MSN Explorer at http://explorer.msn.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] fscanf syntax
Thanks Chris, That worked great. I didn't know that a file could be read directly into an array. --Pat "Chris Fry" <[EMAIL PROTECTED]> wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > Pat, > > You could read the file into an array; > > $aryNessus = file("/pathtofile/filename", "r"); > > Each row of the array then has one line from the file; > Get the record count; > $intNumRecs = count($aryNessus); > > Now read through the array and extract the values; > > for($i=0;$i<=$intNumRecs;$i++) { > $aryNessusFields = explode("|", $aryNessus[$i]); > $hostname = $aryNessusFields[0]; > $protocol = $aryNessusFields[1]; > $number = $aryNessusFields[2]; > $severity = $aryNessusFields[3]; > $description + $aryNessusFields[4]; > // Save the data into the database here > } > > > Regards > > Chris > > Patrick Brown wrote: > > > I want to parse a file with the following syntax. > > > > hostname|protocol|number|severity|description > > > > Each record is on it's own line and have the fields delimited by the pipe > > character |. The file may have thousands of records and I would like to > > bring them into a database for reporting. > > > > Some of you may recognize this a the file format of a Nessus security scan > > file. > > > > What function should I use and what would the syntax be? > > > > Thanks, > > Pat > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > -- > Chris Fry > Quillsoft Pty Ltd > Specialists in Secure Internet Services and E-Commerce Solutions > 10 Gray Street > Kogarah > NSW 2217 > Australia > > Phone: +61 2 9553 1691 > Fax: +61 2 9553 1692 > Mobile: 0419 414 323 > eMail: [EMAIL PROTECTED] > http://www.quillsoft.com.au > > You can download our Public CA Certificate from:- > https://ca.secureanywhere.com/htdocs/cacert.crt > > ** > > This information contains confidential information intended only for > the use of the authorised recipient. If you are not an authorised > recipient of this e-mail, please contact Quillsoft Pty Ltd by return > e-mail. > In this case, you should not read, print, re-transmit, store or act > in reliance on this e-mail or any attachments, and should destroy all > copies of them. > This e-mail and any attachments may also contain copyright material > belonging to Quillsoft Pty Ltd. > The views expressed in this e-mail or attachments are the views of > the author and not the views of Quillsoft Pty Ltd. > You should only deal with the material contained in this e-mail if > you are authorised to do so. > > This notice should not be removed. > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] fscanf syntax
Pat, You could read the file into an array; $aryNessus = file("/pathtofile/filename", "r"); Each row of the array then has one line from the file; Get the record count; $intNumRecs = count($aryNessus); Now read through the array and extract the values; for($i=0;$i<=$intNumRecs;$i++) { $aryNessusFields = explode("|", $aryNessus[$i]); $hostname = $aryNessusFields[0]; $protocol = $aryNessusFields[1]; $number = $aryNessusFields[2]; $severity = $aryNessusFields[3]; $description + $aryNessusFields[4]; // Save the data into the database here } Regards Chris Patrick Brown wrote: > I want to parse a file with the following syntax. > > hostname|protocol|number|severity|description > > Each record is on it's own line and have the fields delimited by the pipe > character |. The file may have thousands of records and I would like to > bring them into a database for reporting. > > Some of you may recognize this a the file format of a Nessus security scan > file. > > What function should I use and what would the syntax be? > > Thanks, > Pat > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] -- Chris Fry Quillsoft Pty Ltd Specialists in Secure Internet Services and E-Commerce Solutions 10 Gray Street Kogarah NSW 2217 Australia Phone: +61 2 9553 1691 Fax: +61 2 9553 1692 Mobile: 0419 414 323 eMail: [EMAIL PROTECTED] http://www.quillsoft.com.au You can download our Public CA Certificate from:- https://ca.secureanywhere.com/htdocs/cacert.crt ** This information contains confidential information intended only for the use of the authorised recipient. If you are not an authorised recipient of this e-mail, please contact Quillsoft Pty Ltd by return e-mail. In this case, you should not read, print, re-transmit, store or act in reliance on this e-mail or any attachments, and should destroy all copies of them. This e-mail and any attachments may also contain copyright material belonging to Quillsoft Pty Ltd. The views expressed in this e-mail or attachments are the views of the author and not the views of Quillsoft Pty Ltd. You should only deal with the material contained in this e-mail if you are authorised to do so. This notice should not be removed. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] fscanf syntax
Hi Patrick, @ 9:04:59 PM on 3/30/2001, Patrick Brown wrote: ... > hostname|protocol|number|severity|description > Each record is on it's own line and have the fields delimited by the pipe > character |. The file may have thousands of records and I would like to > bring them into a database for reporting. ... > What function should I use and what would the syntax be? Try explode(). -Brian -- PGP is spoken here: 0xE4D0C7C8 Please do not carbon copy me on list replies. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] fscanf syntax
I want to parse a file with the following syntax. hostname|protocol|number|severity|description Each record is on it's own line and have the fields delimited by the pipe character |. The file may have thousands of records and I would like to bring them into a database for reporting. Some of you may recognize this a the file format of a Nessus security scan file. What function should I use and what would the syntax be? Thanks, Pat -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] fscanf
- Original Message - From: "Kubol" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Tuesday, March 13, 2001 12:48 AM Subject: Re: [PHP] fscanf > 01-03-13 05:00:01, "Yasuo Ohgaki" <[EMAIL PROTECTED]> wrote: > > >I think fscanf() is line oriented in C, isn't it? (I haven't used fscanf() for a > >long time, I'm not 100% sure w/o reference.) > no, i'm sure in c fscanf reads the next string available, not first string in next line > >Your $buffer should only have "lenin". > i agree. in first iteration buffer should contain "lenin", but in second the content should be "lives", not "forever", > but is "forever" and i dont > know why and i'm angry about it :-) > >while (fscanf($file,"%s %s",$buf1,buf2)) > >You should get "lives" in $buf2. > i agree again, but i dont know how many stings do i have in one line. therefore i cant use > fscanf($file,"n*%s",n*some_variables); > besides it looks terrible... :-) Now I understand why you are wondering. In C, you can get "words" in file with your code, but not in PHP. fscanf() between C and PHP is not identical when I made a simple test codes for both. It seems PHP's fscanf() advances file pointer to end of line while C's fscanf() must be done by programmers like fscanf(fp,"%*[^\n]"). In most cases, programmers want to move file pointer to the end of line. Maybe that's why PHP's fscanf() does this. We cannot expect PHP functions works just like C/C++/Java since it is different languange even is name of the function is the same. I guess what you want to do is getting all words from a file using fscanf(), but it seems it is not possible with PHP's fscanf(). (Or is it a bug? Anyone?) Functions that have the same name in C better to be behaive just like C in PHP for C programmers. (but may not be good for non-C programmers.) I'm relatively new to PHP, so I don't know historical reason why some functions does not behaive like C. I see a lot of differences PHP and C, I think this is the way it is. How about a start new thread if you still curious about fscanf() behaviour? Someone may be able to give better explaination. Regards, -- Yasuo Ohgaki > regards, > jakub zytka > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] fscanf
01-03-13 05:00:01, "Yasuo Ohgaki" <[EMAIL PROTECTED]> wrote: >I think fscanf() is line oriented in C, isn't it? (I haven't used fscanf() for a >long time, I'm not 100% sure w/o reference.) no, i'm sure in c fscanf reads the next string available, not first string in next line >Your $buffer should only have "lenin". i agree. in first iteration buffer should contain "lenin", but in second the content should be "lives", not "forever", but is "forever" and i dont know why and i'm angry about it :-) >while (fscanf($file,"%s %s",$buf1,buf2)) >You should get "lives" in $buf2. i agree again, but i dont know how many stings do i have in one line. therefore i cant use fscanf($file,"n*%s",n*some_variables); besides it looks terrible... :-) regards, jakub zytka -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] fscanf
"Kubol" <[EMAIL PROTECTED]> wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... > hello > > if this is really a silly problem dont blame me, i'm a newbie in php (but i'm experienced in c++ a bit :-) > well, imagine i want to write a content of some text file word by word. example will be more understandable > than my english: > let file.txt consists: > lenin lives > forever > i want my php script to write to the screen: > lenin > lives > forever > I tried to manage this using fscanf function. i expected it works same as in c, but it looks it doesnt. consider > the code: > while (fscanf($file,"%s",$buffer)==1) > { > echo $buffer,""; > } > the result: > lenin > forever > just like fscanf moved the file pointer to the next line after reading "lenin". i dont know whats up... why "lives" > disappeared??? i know i can try using fgets and then split the string, but i want to know why fscanf doesnt > work... :-) > could anyone explain me this phenomena? I think fscanf() is line oriented in C, isn't it? (I haven't used fscanf() for a long time, I'm not 100% sure w/o reference.) "%s" format in fscanf() and sscanf() will macthes sequecne of non-white-space charactors in C and PHP. Your $buffer should only have "lenin". So if you write like while (fscanf($file,"%s %s",$buf1,buf2)) You should get "lives" in $buf2. Regards, Yasuo Ohgaki > > best regards, > jakub zytka > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] fscanf
hello if this is really a silly problem dont blame me, i'm a newbie in php (but i'm experienced in c++ a bit :-) well, imagine i want to write a content of some text file word by word. example will be more understandable than my english: let file.txt consists: lenin lives forever i want my php script to write to the screen: lenin lives forever I tried to manage this using fscanf function. i expected it works same as in c, but it looks it doesnt. consider the code: while (fscanf($file,"%s",$buffer)==1) { echo $buffer,""; } the result: lenin forever just like fscanf moved the file pointer to the next line after reading "lenin". i dont know whats up... why "lives" disappeared??? i know i can try using fgets and then split the string, but i want to know why fscanf doesnt work... :-) could anyone explain me this phenomena? best regards, jakub zytka -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] fscanf problem
Hmm..noone has any ideas on this one? Its really driving me crazy - John Vanderbeck - Admin, GameDesign - Original Message - From: "John Vanderbeck" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Sunday, February 11, 2001 10:01 PM Subject: [PHP] fscanf problem > my code... > > WRITE TO FILE: > if ($item_file = fopen("$thisdir/item.dat", "w")) > { > fwrite($item_file, "$new_item\n$asin\n$description\namazon"); > fclose($item_file); > echo "$new_item created"; > } > > READ FROM FILE: >$item_file = fopen("$thisdir/item.dat", "r"); >fscanf($item_file, "%s\n%s\n%s\n%s", $display, $asin, $description, > $link); >fclose($item_file); > > PROBLEM: > $display comes out correct, but $asin, $descrition and $link are always > empty > > SAMPLE FILE: > MyItem > 11517 > ThisIsMyDecriptionWithoutSpaces > amazon > > - John Vanderbeck > - Admin, GameDesign > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] fscanf problem
my code... WRITE TO FILE: if ($item_file = fopen("$thisdir/item.dat", "w")) { fwrite($item_file, "$new_item\n$asin\n$description\namazon"); fclose($item_file); echo "$new_item created"; } READ FROM FILE: $item_file = fopen("$thisdir/item.dat", "r"); fscanf($item_file, "%s\n%s\n%s\n%s", $display, $asin, $description, $link); fclose($item_file); PROBLEM: $display comes out correct, but $asin, $descrition and $link are always empty SAMPLE FILE: MyItem 11517 ThisIsMyDecriptionWithoutSpaces amazon - John Vanderbeck - Admin, GameDesign -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]