PortableFtruncate
提供:やる気向上作戦
ftruncate for Win32
fd から HANDLEへの変換には、VCなら _get_osfhandleが使える。
// If you are using Visual C++, you can use _get_osfhandle() (declared in <io.h>)
// to convert a file descriptor (int) into a file handle (HANDLE)
int ftruncate(HANDLE hFile, long long size)
{
LARGE_INTEGER currentpos, pos, newpos;
// save current position
pos.QuadPart = 0;
if (!SetFilePointerEx(hFile, pos, ¤tpos, FILE_CURRENT)) {
return -1;
}
// move to new position
pos.QuadPart = size;
if (!SetFilePointerEx(hFile, pos, &newpos, FILE_BEGIN)) {
return -1;
}
// change file size
if (!SetEndOfFile(hFile)) {
return -1;
}
// ...and return to the original position
// (the file pointer is not changed by ftruncate on POSIX systems.)
if (!SetFilePointerEx(hFile, currentpos, &newpos, FILE_BEGIN)) {
return -1;
}
return 0;
}