<> wrote:
> On 8 16 , 3 23 , (Jens Thoms Toerring) wrote:
> > webinfin...@gmail.com <webinfin...@gmail.com> wrote:
> > > #include <stdio.h>
> > > int main(){
> > > FILE *fp;
> > > while(1){
> > > fp = fopen("foo.txt", "r"); ;
> > > if (fp != NULL)
> > > break;
> > > else{
> > > printf("Waiting for the file\n");
> > > }
> > > }
> > > printf("I am out.\n");
> > > fclose(fp);
> > > return 0;
> > > }
> > > foo.txt is generated after this code starts to execute. My problem
> > > here is that even after foo.txt has been generated, the code is still
> > > in a tight loop printing:
>
> > But perhaps the other application that creates the file creates
> > it with write-only permissions and only changes that once it's
> > done with writing everything it wants to the file? Of course
> > this is pure guesswork since there is no information to go by
> > except what you write, but it might be useful to check (with
> > probably system-specific functions, e.g. if you're running
> > UNIX you could check what errno tells you about the reasons)
> > why fopen() fails for that long.
>
> It takes the code more than 10 seconds to get the file. I would like
> to find a solution that when you generate the file, it will be
> captured by the code immediately. Is there a way? Thanks.
There's nothing in your program that would keep it form doing so
unless there are additional circumstances that would make fopen()
fail. One of those could be that the other program opens the file
with permissions that don't allow your program to open it for
reading - but you're the only one who can check for that. To
repeat: in your program fopen() should return successfully once
the file exists and it's permitted to open the file for reading.
One of these two conditions must not be satisfied for the 10
seconds you're writing about and only you have the possibility
to find out what it is - therefore I did recommend to resort to
system-specific methods (the C standard only requires that
fopen() returns NULL on failure but does not specify any methods
to obtain further informations) to determine why fopen() did fail,
those methods may tell you that the file either doesn't exist
when you assume it does or that you for some time don't have the
permissions to open it for reading.
Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________
http://toerring.de