"nass" <> wrote in message
news: ups.com...
> hello everyone i am looking into asynchronous file writing
> (appending).
All OT for clc. Try comp.unix.programmer.
> what the program is doing:
> i am running a loop and when the condition is met (basically a timer)
> a buffer gets appended to a file. every so often (another timer) the
> filename is changed so the next write must create a new file and start
> appending into that. it is really a timed Binary log.
>
> i have managed to do that 'saving' with stream to file operations
> (where file is of type fstream), and i have also managed to do it with
> fopen(). but it is a requirement that the execution is not halted so i
> looked into threads... and then i came across asynchronous write.
>
> now looking into aio_write and the struct aiocb that it takes as input
> argument, i see that i must provide a file descriptor instead of ther
> usual FILE *, that fopen returns.
Once you start playing in the POSIX world, it's best to stay there. i.e.
use open() instead of fopen(). Of course, that puts you completely
off-topic for here.
> and i am wondering if i should be opening the file prior to
> aio_write(), or just opening the file once for evey new filename (*ie
> when the file doesnt exist), getting the file descriptor, and closing
> the file, so that when the time for aio_write() comes , aio_write()
> will open, write and close the file on its own.
No, it won't. aio_write() works just like write(), except it may be
asynchronous. Once you close a fd, it becomes invalid; you can't just keep
writing to it.
I don't think you want AIO here at all. Just create a standard FILE*, stuff
it in a global variable, and fwrite() to it from the first timer's function.
The second timer's function would fopen() the next file, change the global
to the new file, and fclose() the old file. There's probably a race
condition or reentrancy issue in there somewhere, though, which is why you
need to talk to folks who work with threads on a regular basis (i.e. not on
clc).
S
--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov
--
Posted via a free Usenet account from
http://www.teranews.com