views:

212

answers:

2

Hi

Is it possible in C++ to somehow get a pointer to the beginning of an opened file so that it ( the pointer ) can be passed to the unix write function together with the size of the file?

Just to be clear: I want to pass the whole file to a write-like function - how do I do this?

+4  A: 

You can just mmap() the complete file, and then pass this pointer to write. You'll have to open it using the open(), not fopen() though.

Anteru
and get the length of the file from the fstat(2) system call after you've opened the file.
camh
+4  A: 

sendfile() is specifically for this purpose on POSIX platforms when sending over the network, and the kernel handles the intricacies of doing it.

Will
Sendfile is (a) not portable; (b) only supported for some destinations (e.g., only sockets)
derobert
This bypasses userspace! Meaning you cannot actually do anything with the data, you can just order the kernel to copy it around! The question is unclear if that is all that should be done. If it is, this should be the fastest possible way.
AndreasT
"not portable" irked me somewhat and I did a lookup. You are absolutely right! You can only use this function in the linux world, other unixoids or windows don't know this command or implement it differently. Still, if you target linux only, it should be the best possible way.
AndreasT
Case is important in C/unix - it should be sendfile()
camh