fclose

This is a discussion on fclose within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hello, I have these lines: $fp = "test.txt"; if ($datei = fopen ($fp, "r")) { while (!feof ($datei)) { $...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-28-2004
Neudeck Werner
 
Posts: n/a
Default fclose

Hello,
I have these lines:
$fp = "test.txt";
if ($datei = fopen ($fp, "r")) {
while (!feof ($datei)) {
$zeichen = fgetc ($datei);
echo ($zeichen);
}
fclose($fp);
}

The file is opened correctly and the content of the file is shown. But
below the content there is the error:
Warning: fclose(): supplied argument is not a valid stream resource in
........... on line 9
Where is the mistake?
Thanks Werner

Reply With Quote
  #2 (permalink)  
Old 10-28-2004
Alvaro G Vicario
 
Posts: n/a
Default Re: fclose

*** Neudeck Werner wrote/escribió (Thu, 28 Oct 2004 13:17:01 +0200):
> $fp = "test.txt";
> if ($datei = fopen ($fp, "r")) {

[...]
> fclose($fp);


> The file is opened correctly and the content of the file is shown. But
> below the content there is the error:
> Warning: fclose(): supplied argument is not a valid stream resource in
> .......... on line 9
> Where is the mistake?


The error is that $fp is not a valid stream resource. Actually, $fp is a string. Let's check the manual:

Usage: resource fopen ( string filename, string mode [, int use_include_path [, resource zcontext]] )
Purpose: Opens file or URL

Usage: bool fclose ( resource handle )
Purpose: Closes an open file pointer

So fclose() doesn't expect a string as an argument. Can you see now where to get a resouce handle from?

--
-- Álvaro G. Vicario - Burgos, Spain
-- Thank you for not e-mailing me your questions
--
Reply With Quote
  #3 (permalink)  
Old 10-28-2004
Neudeck Werner
 
Posts: n/a
Default Re: fclose

Hello,

"Alvaro G Vicario" <alvaro_QUITAR_REMOVE@telecomputeronline.com>
schrieb im Newsbeitrag
news:kym8ry84ek04.k98h53kwusdt$.dlg@40tude.net...
> *** Neudeck Werner wrote/escribió (Thu, 28 Oct 2004 13:17:01 +0200):
>> $fp = "test.txt";
>> if ($datei = fopen ($fp, "r")) {

> [...]
>> fclose($fp);

>
>> The file is opened correctly and the content of the file is shown.
>> But
>> below the content there is the error:
>> Warning: fclose(): supplied argument is not a valid stream resource
>> in
>> .......... on line 9
>> Where is the mistake?

>
> The error is that $fp is not a valid stream resource. Actually, $fp
> is a string. Let's check the manual:
>
> Usage: resource fopen ( string filename, string mode [, int
> use_include_path [, resource zcontext]] )
> Purpose: Opens file or URL
>
> Usage: bool fclose ( resource handle )
> Purpose: Closes an open file pointer
>
> So fclose() doesn't expect a string as an argument. Can you see now
> where to get a resouce handle from?
>

first: I'm a beginner so I have taken the example out of a book and
there it was written exactly like above.
Now I have modified it, like you said and it works.
Thanks, Werner

Reply With Quote
  #4 (permalink)  
Old 10-28-2004
Alvaro G Vicario
 
Posts: n/a
Default Re: fclose

*** Neudeck Werner wrote/escribió (Thu, 28 Oct 2004 14:05:02 +0200):
> first: I'm a beginner so I have taken the example out of a book and
> there it was written exactly like above.


For some reason all programming books have typos in basic examples :)

--
-- Álvaro G. Vicario - Burgos, Spain
-- Thank you for not e-mailing me your questions
--
Reply With Quote
  #5 (permalink)  
Old 10-28-2004
yann kloniecki
 
Posts: n/a
Default Re: fclose

of course, this does'nt work because fclose wait the handle of the file open
with fopen !
if ($datei = fopen ($fp, "r")) this is wrong.....
$datei is the handle of the open file...
$handle=fopen ($fp, "r");
....... work on file
fclose($handle);

yann.


"Neudeck Werner" <w.neudeck@web.de> a écrit dans le message de news:
clqnh5$1b5$1@ngspool-d02.news.aol.com...
> Hello,
>
> "Alvaro G Vicario" <alvaro_QUITAR_REMOVE@telecomputeronline.com> schrieb
> im Newsbeitrag news:kym8ry84ek04.k98h53kwusdt$.dlg@40tude.net...
>> *** Neudeck Werner wrote/escribió (Thu, 28 Oct 2004 13:17:01 +0200):
>>> $fp = "test.txt";
>>> if ($datei = fopen ($fp, "r")) {

>> [...]
>>> fclose($fp);

>>
>>> The file is opened correctly and the content of the file is shown. But
>>> below the content there is the error:
>>> Warning: fclose(): supplied argument is not a valid stream resource in
>>> .......... on line 9
>>> Where is the mistake?

>>
>> The error is that $fp is not a valid stream resource. Actually, $fp is a
>> string. Let's check the manual:
>>
>> Usage: resource fopen ( string filename, string mode [, int
>> use_include_path [, resource zcontext]] )
>> Purpose: Opens file or URL
>>
>> Usage: bool fclose ( resource handle )
>> Purpose: Closes an open file pointer
>>
>> So fclose() doesn't expect a string as an argument. Can you see now where
>> to get a resouce handle from?
>>

> first: I'm a beginner so I have taken the example out of a book and there
> it was written exactly like above.
> Now I have modified it, like you said and it works.
> Thanks, Werner



Reply With Quote
  #6 (permalink)  
Old 10-29-2004
Neudeck Werner
 
Posts: n/a
Default Re: fclose

Hello yann,

"yann kloniecki" <yklo@club-internet.fr> schrieb im Newsbeitrag
news:41811cec$0$15755$7a628cd7@news.club-internet.fr...
> of course, this does'nt work because fclose wait the handle of the
> file open with fopen !
> if ($datei = fopen ($fp, "r")) this is wrong.....
> $datei is the handle of the open file...
> $handle=fopen ($fp, "r");
> ...... work on file
> fclose($handle);
>
> yann.
>
>
> "Neudeck Werner" <w.neudeck@web.de> a écrit dans le message de news:
> clqnh5$1b5$1@ngspool-d02.news.aol.com...
>> Hello,

...........
>> "Alvaro G Vicario" <alvaro_QUITAR_REMOVE@telecomputeronline.com>
>> schrieb im Newsbeitrag
>> news:kym8ry84ek04.k98h53kwusdt$.dlg@40tude.net...
>>> *** Neudeck Werner wrote/escribió (Thu, 28 Oct 2004 13:17:01
>>> +0200):

...........

in the meantime I have understood it completeley, thanks to you and
Alvaro.
After reading your postings and studying the manual, everything works.
I only wonder, why there is such an example in a book for beginners.
And in the meantime I have realized, that it's not the only mistake in
this book (the name ist "PHP4 for dummies")
Thanks to all
Werner

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:02 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0