Is it possible to know how many bytes have been printed to a file stream such as standard output? Is it possible to know how many bytes have been printed to a file stream such as standard output? unix unix

Is it possible to know how many bytes have been printed to a file stream such as standard output?


POSIX-specific: redirect stdout to a file, flush after all writing is done, then stat the file and look at st_size (or use the ls command).

Update: You say you're trying to control the quantity of output of a program. The POSIX head command will do that. If that's not satisfactory, then state your requirements clearly.


It's rather a heavyweight solution, but the following will work:

  1. Create a pipe by calling pipe()
  2. Spawn a child process
  3. In the parent: redirect stdout to the write-side of the pipe, and close the read side (and the old stdout)
  4. In the child: keep reading from the read side of the pipe, and copying the data to the inherited stdout (which is the original stdout) - counting it as it goes past
  5. In the parent, keep writing to stdout (which is now the pipe) as usual
  6. Use some form of IPC to communicate the result of the count from the child to the parent.

Basically the idea is to spawn a child process, and pipe all output through it, and have the child process count all the data as it goes through.

The precise form of IPC to use may vary - for example, shared memory (with atomic reads/writes on each side) would work well for fast transfer of data, but other methods (such as sockets, more pipes etc) are possible, and offer better scope for synchronisation.

The trickiest part is the synchronisation, i.e. ensuring that, at the time the child tells the parent how much data has been written, it has already processed all the data that the parent said (and there is none left in the pipe, for example). How important this is will depend on exactly what your aim is - if an approximate indication is all that's required, then you may be able to get away with using shared memory for IPC and not performing any explicit synchronisation; if the total is only required at the end then you can close stdout from the parent, and have the child indicate in the shared memory when it has received the eof notification.

If you require more frequent readouts, which must be exact, then something more copmlex will be required, but this can be achieved by designing some sort of protocol using sockets, pipes, or even condvars/semaphores/etc in the shared memory.


printf returns the number of bytes written.

Add them up.