Reading from file

Reading from file

am 02.11.2010 04:34:35 von Ethan Rosenberg

Dear List -

Thank you for all your help. Here is another one.....

I have a file [/home/ethan/PHP/RecNum] which consists of the number
1005. It also has been "1005". No matter how I do it, it is only
recognized as 1.

Here is the code. In this case, the file is "1005".

$fptr1 = fopen("/home/ethan/PHP/RecNum", "r+");
$recNum = (int)fscanf($fptr1,"%s");
echo $recNum; //which always is 1

If I do not typecast $recNum to int, the value is Array.

Thanks.

Ethan

MySQL 5.1 PHP 5 Linux [Debian (sid)]



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

Re: Reading from file

am 02.11.2010 05:31:30 von dmagick

On 02/11/10 14:34, Ethan Rosenberg wrote:
> Dear List -
>
> Thank you for all your help. Here is another one.....
>
> I have a file [/home/ethan/PHP/RecNum] which consists of the number
> 1005. It also has been "1005". No matter how I do it, it is only
> recognized as 1.
>
> Here is the code. In this case, the file is "1005".
>
> $fptr1 = fopen("/home/ethan/PHP/RecNum", "r+");
> $recNum = (int)fscanf($fptr1,"%s");
> echo $recNum; //which always is 1
>
> If I do not typecast $recNum to int, the value is Array.

Right, which is what the manual says as well:

http://www.php.net/fscanf

If only two parameters were passed to this function, the values parsed
will be returned as an array.

So you can either do:

$line = fscanf($fptr1, "%s");
$recNum = $line[0]; // get the first element in the array as $recNum

or

while ($line = fscanf($fptr1, "%s\n")) {
$recNum = $line[0];
}

--
Postgresql & php tutorials
http://www.designmagick.com/


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

Re: Reading from file

am 02.11.2010 11:08:18 von Artur Ejsmont

"Return Values

If only two parameters were passed to this function, the values parsed
will be returned as an array. Otherwise, if optional parameters are
passed, the function will return the number of assigned values. The
optional parameters must be passed by reference."

http://php.net/manual/en/function.fscanf.php

so you should expect an array. print_r it to see what is inside.

art


On 2 November 2010 03:34, Ethan Rosenberg wrote:
> Dear List -
>
> Thank you for all your help. =A0Here is another one.....
>
> I have a file [/home/ethan/PHP/RecNum] which consists of the number 1005.
> =A0It also has been "1005". =A0No matter how I do it, it is only recogniz=
ed as
> 1.
>
> Here is the code. =A0In this case, the file is "1005".
>
> $fptr1 =3D fopen("/home/ethan/PHP/RecNum", "r+");
> $recNum =3D (int)fscanf($fptr1,"%s");
> echo $recNum; =A0//which always is 1
>
> If I do not typecast $recNum to int, the value is Array.
>
> Thanks.
>
> Ethan
>
> MySQL 5.1 =A0PHP 5 =A0Linux [Debian (sid)]
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



--=20
Visit me at:
http://artur.ejsmont.org/blog/

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

Re: Reading from file

am 02.11.2010 15:07:21 von Ethan Rosenberg

At 06:08 AM 11/2/2010, Artur Ejsmont wrote:
>"Return Values
>
>If only two parameters were passed to this function, the values parsed
>will be returned as an array. Otherwise, if optional parameters are
>passed, the function will return the number of assigned values. The
>optional parameters must be passed by reference."
>
>
>
>so you should expect an array. print_r it to see what is inside.
>
>art
>
>
>On 2 November 2010 03:34, Ethan Rosenberg wrote:
> > Dear List -
> >
> > Thank you for all your help. Here is another one.....
> >
> > I have a file [/home/ethan/PHP/RecNum] which consists of the number 1005.
> > It also has been "1005". No matter how I do it, it is only recognized as
> > 1.
> >
> > Here is the code. In this case, the file is "1005".
> >
> > $fptr1 = fopen("/home/ethan/PHP/RecNum", "r+");
> > $recNum = (int)fscanf($fptr1,"%s");
> > echo $recNum; //which always is 1
> >
> > If I do not typecast $recNum to int, the value is Array.
> >
> > Thanks.
> >
> > Ethan
> >
> > MySQL 5.1 PHP 5 Linux [Debian (sid)]
> >
> >
==============
Artur -

Thanks.

I also wanted to be able to increment recNum.

Her is what finally worked for me:

$fptr1 = fopen("/home/ethan/PHP/RecNum", "r+");
$recNum = fscanf($fptr1,"%d");
$sql1 = "insert into intake2 (Site,Record,BMI) values ('A',$recNum[0],19)";
$recNum[0] = $recNum[0] + 1;
rewind($fptr1);
fprintf($fptr1,"%d", $recNum[0]);

Ethan



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