Re: how to read a file newly generated in real time?
- From: Kenneth Brody <kenbrody@xxxxxxxxxxx>
- Date: Fri, 17 Aug 2007 10:35:52 -0400
"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>
.
- References:
- how to read a file newly generated in real time?
- From: webinfinite@xxxxxxxxx
- how to read a file newly generated in real time?
- Prev by Date: Re: best GUI library for vector drawing program
- Next by Date: Re: Read-only functionality without 'const'
- Previous by thread: Re: how to read a file newly generated in real time?
- Next by thread: type conversion within a union
- Index(es):
Relevant Pages
|
Loading