Re: how to read a file newly generated in real time?



"webinfinite@xxxxxxxxx" wrote:
[...]
while(1){
fp = fopen("foo.txt", "r"); ;
if (fp != NULL)
break;
else{
printf("Waiting for the file\n");
}
}
[...]
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:

Waiting for the file
Waiting for the file
Waiting for the file
Waiting for the file
Waiting for the file
Waiting for the file

Until long after the file is generated, the code comes out. Is there
any solution to force the code to find out if this file has been
generated?

How long is "long after"?

Your program may be running in such a tight loop that it is sending
output faster than the output device can handle, and it takes that
long to simply finish displaying the already-printf'ed messages.

Add some code (outside the scope of clc) to slow it down. Perhaps
add a 1/10th second delay at the end of the loop.

Or only print your message after some time has elapsed. You can
use time(), and only print if the value is different from the last
call to time().

For example, here is some (untested!) code:

if ( ClockHasChanged() )
printf("Waiting for the file\n");

...

int ClockHasChanged()
{
static time_t prev = 0;
time_t now = time(NULL);

if ( prev != now )
{
prev = now;
return 1;
}

return 0;
}

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsASpamTrap@xxxxxxxxx>

.



Relevant Pages

  • Re: Protecting a HD
    ... Is there a way to make you wait more than two minute before posting ... the exact same question again? ... No waiting no answers provided. ... Prev by Date: ...
    (microsoft.public.windowsxp.general)
  • Re: John McCririck attacks Blair...
    ... > But I'm still waiting for something from you with meat in it. ... Don't hold your breath ... ... Prev by Date: ...
    (uk.politics.misc)
  • Re: Down stairs in Gehennom not found
    ... Boudewijn. ... "I have hundreds of other quotes, just waiting to replace this one ... Prev by Date: ...
    (rec.games.roguelike.nethack)
  • Re: average
    ... > automate the process. ... > Waiting for reply. ... Prev by Date: ...
    (microsoft.public.mac.office.excel)
  • Re: Infinite Crisis not shipping?
    ... pate at once? ... But still I cant see a book like IC possibly not being ready to go at ... this pont.And I don't think I could stand waiting even one week more! ... Prev by Date: ...
    (rec.arts.comics.dc.universe)

Loading