本文整理汇总了C++中SetEndOfFile函数的典型用法代码示例。如果您正苦于以下问题:C++ SetEndOfFile函数的具体用法?C++ SetEndOfFile怎么用?C++ SetEndOfFile使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SetEndOfFile函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: create_very_big_file
/**
* @brief
* @param
* @see
* @remarks
* @code
* @endcode
* @return
**/
bool create_very_big_file(_In_ const wchar_t* file_path, _In_ uint64_t size_in_mb)
{
_ASSERTE(NULL != file_path);
if (NULL == file_path) return false;
if (is_file_existsW(file_path))
{
::DeleteFileW(file_path);
}
// create very big file
HANDLE file_handle = CreateFile(
file_path,
GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
CREATE_NEW,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (INVALID_HANDLE_VALUE == file_handle)
{
print("err ] CreateFile( %ws ) failed. gle = %u", file_path, GetLastError());
return false;
}
LARGE_INTEGER file_size = { 0 };
//file_size.LowPart = 0;
//file_size.HighPart = 1;
file_size.QuadPart = (1024 * 1024) * size_in_mb;
if (!SetFilePointerEx(file_handle, file_size, NULL, FILE_BEGIN))
{
print("err ] SetFilePointerEx() failed. gle = %u", GetLastError());
CloseHandle(file_handle);
return false;
}
SetEndOfFile(file_handle);
CloseHandle(file_handle);
return true;
}
示例2: myFlushViewOfFile
CMappedMemory::~CMappedMemory()
{
if (m_Base)
{
myFlushViewOfFile(m_Base, NULL);
myUnmapViewOfFile(m_Base);
}
if (m_FileMapping)
CloseHandle(m_FileMapping);
if (m_DirectFile)
{
if (INVALID_SET_FILE_POINTER != SetFilePointer(m_DirectFile, m_Size, NULL, FILE_BEGIN))
SetEndOfFile(m_DirectFile);
FlushFileBuffers(m_DirectFile);
CloseHandle(m_DirectFile);
}
}
示例3: ASSERT
ALERROR CTextFileLog::Create (BOOL bAppend)
// Create
//
// Create a new log file
{
ASSERT(m_hFile == NULL);
m_hFile = CreateFile(m_sFilename.GetASCIIZPointer(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (m_hFile == INVALID_HANDLE_VALUE)
{
DWORD dwError = ::GetLastError();
m_hFile = NULL;
return ERR_FAIL;
}
// If we're appending to an existing log file, move the file pointer
// to the end of the file.
if (bAppend)
{
LONG lFileHigh = 0;
m_dwSessionStart = ::SetFilePointer(m_hFile, 0, &lFileHigh, FILE_END);
}
// Otherwise, truncate the file
else
{
SetFilePointer(m_hFile, 0, NULL, FILE_BEGIN);
SetEndOfFile(m_hFile);
m_dwSessionStart = 0;
}
return NOERROR;
}
示例4: como__ftruncate
// win32 ftruncate emulation using lseek then
// setting endoffile position
int como__ftruncate (int fd, long pos){
if ( lseek(fd, pos, SEEK_SET) < 0 ){
return -1;
}
int handle = _get_osfhandle(fd);
if (handle == -1){
errno = EBADF;
return -1;
}
if (SetEndOfFile((HANDLE)handle) == 0){
errno = GetLastError();
return -1;
}
return 0;
}
示例5: AllocateFileRange
/**
* this function tries to make a particular range of a file allocated (corresponding to disk space)
* it is advisory, and the range specified in the arguments will never contain live data
*/
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
#if defined(WIN32)
// Windows-specific version
HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
LARGE_INTEGER nFileSize;
int64_t nEndPos = (int64_t)offset + length;
nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
nFileSize.u.HighPart = nEndPos >> 32;
SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
SetEndOfFile(hFile);
#elif defined(MAC_OSX)
// OSX specific version
fstore_t fst;
fst.fst_flags = F_ALLOCATECONTIG;
fst.fst_posmode = F_PEOFPOSMODE;
fst.fst_offset = 0;
fst.fst_length = (off_t)offset + length;
fst.fst_bytesalloc = 0;
if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
fst.fst_flags = F_ALLOCATEALL;
fcntl(fileno(file), F_PREALLOCATE, &fst);
}
ftruncate(fileno(file), fst.fst_length);
#elif defined(__linux__)
// Version using posix_fallocate
off_t nEndPos = (off_t)offset + length;
posix_fallocate(fileno(file), 0, nEndPos);
#else
// Fallback version
// TODO: just write one byte per block
static const char buf[65536] = {};
if (fseek(file, offset, SEEK_SET)) {
return;
}
while (length > 0) {
unsigned int now = 65536;
if (length < now)
now = length;
fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
length -= now;
}
#endif
}
示例6: strncpy
File::File(char *pFileName, int length) {
DWORD ret = 0;
/* Make a copy of the filename */
strncpy(fileName, pFileName, MAX_PATH);
hFile = INVALID_HANDLE_VALUE;
/* Open the file - Note its opened write, shared read, and RANDOM_ACCESS since
we are going to be jumping around alot */
hFile = CreateFile(fileName, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
ret = GetLastError();
throwError(fileName, ret);
}
/* Now make sure the file is the correct length */
ret = SetFilePointer(hFile, (DWORD) length, NULL, FILE_BEGIN);
if (ret == INVALID_SET_FILE_POINTER) {
ret = GetLastError();
CloseHandle(hFile);
hFile = NULL;
throwError(fileName, ret);
}
/* Now save the file as this length */
ret = SetEndOfFile(hFile);
if (ret==0) {
ret = GetLastError();
CloseHandle(hFile);
hFile = NULL;
throwError(fileName, ret);
}
/* Initialize the critical section */
InitializeCriticalSection (&fileLock);
}
示例7: lock
BOOL PrunnedFileViewer::Show(LPCTSTR description)
{
if (m_hFile == INVALID_HANDLE_VALUE)
return FALSE;
CThreadLock lock(&m_cs, TRUE);
DWORD dwPtr = SetFilePointer(m_hFile, 0, 0, FILE_BEGIN);
if (dwPtr == INVALID_SET_FILE_POINTER)
{
DWORD dwError = GetLastError();
if (dwError != NO_ERROR)
return FALSE;
}
if (SetEndOfFile(m_hFile) == 0)
{
DWORD dwError = GetLastError();
if (dwError != NO_ERROR)
return FALSE;
}
while (m_debugLines.size() >= m_maxLines)
m_debugLines.pop_front();
WideCharToMultiByte(CP_ACP, NULL, description, -1, bf, cbfLen, 0,0);
DWORD le = GetLastError();
m_debugLines.push_back(bf);//Gives a ERROR_INVALID_HANDLE (6)
if (le != GetLastError())
SetLastError(le);
std::list<std::string>::const_iterator it = m_debugLines.begin();
DWORD bytesWritten = 0;
while (it != m_debugLines.end())
{
DWORD bytesWritten = 0;
const std::string& str = *it;
if (!WriteFile(m_hFile, (LPCSTR)str.c_str(), (DWORD)str.size(), &bytesWritten, NULL))
{
CloseHandle(m_hFile);
m_hFile = INVALID_HANDLE_VALUE;
return FALSE;//FlushFileBuffers(m_hFile);
}
it++;
}
return TRUE;
}
示例8: CheckFileSize
void CheckFileSize(HANDLE hFile, DWORD dwOffset, DWORD dwHighOrder)
{
DWORD dwRc = 0;
DWORD dwError = 0;
LARGE_INTEGER qwFileSize;
dwRc = SetFilePointer(hFile, dwOffset, (PLONG)&dwHighOrder, FILE_BEGIN);
if (dwRc == INVALID_SET_FILE_POINTER)
{
Trace("GetFileSizeEx: ERROR -> Call to SetFilePointer failed with %ld.\n",
GetLastError());
CleanUp(hFile);
Fail("");
}
else
{
if (!SetEndOfFile(hFile))
{
dwError = GetLastError();
CleanUp(hFile);
if (dwError == 112)
{
Fail("GetFileSizeEx: ERROR -> SetEndOfFile failed due to lack of "
"disk space\n");
}
else
{
Fail("GetFileSizeEx: ERROR -> SetEndOfFile call failed "
"with error %ld\n", dwError);
}
}
else
{
GetFileSizeEx(hFile, &qwFileSize);
if ((qwFileSize.u.LowPart != dwOffset) ||
(qwFileSize.u.HighPart != dwHighOrder))
{
CleanUp(hFile);
Fail("GetFileSizeEx: ERROR -> File sizes do not match up.\n");
}
}
}
}
示例9: truncate
int truncate(const char *path, long length)
{
HANDLE file = CreateFile(path, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
FILE_SHARE_WRITE | FILE_SHARE_READ, NULL);
if (file == INVALID_HANDLE_VALUE)
{
return -1;
}
if (SetFilePointer(file, length, NULL, FILE_BEGIN) == 0xFFFFFFFF || !SetEndOfFile(file))
{
CloseHandle(file);
return -1;
}
CloseHandle(file);
return 0;
}
示例10: SaveRecordtoFile
// Save recorded speech to file
int SaveRecordtoFile(const char* fileName, WAVEFORMATEX* wf, HWAVEIN* hWaveIn, WAVEHDR* waveHeader, MMTIME* mmTime)
{
int res;
DWORD NumToWrite=0;
DWORD dwNumber = 0;
waveHeader->dwBytesRecorded = mmTime->u.cb;
HANDLE FileHandle = CreateFile( CString(fileName), GENERIC_WRITE,
FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
dwNumber = FCC("RIFF");
WriteFile(FileHandle, &dwNumber, 4, &NumToWrite, NULL);
dwNumber = waveHeader->dwBytesRecorded + 18 + 20;
WriteFile(FileHandle, &dwNumber, 4, &NumToWrite, NULL);
dwNumber = FCC("WAVE");
WriteFile(FileHandle, &dwNumber, 4, &NumToWrite, NULL);
dwNumber = FCC("fmt ");
WriteFile(FileHandle, &dwNumber, 4, &NumToWrite, NULL);
dwNumber = 18L;
WriteFile(FileHandle, &dwNumber, 4, &NumToWrite, NULL);
WriteFile(FileHandle, wf, sizeof(WAVEFORMATEX), &NumToWrite, NULL);
dwNumber = FCC("data");
WriteFile(FileHandle, &dwNumber, 4, &NumToWrite, NULL);
dwNumber = waveHeader->dwBytesRecorded;
WriteFile(FileHandle, &dwNumber, 4, &NumToWrite, NULL);
WriteFile(FileHandle, waveHeader->lpData, waveHeader->dwBytesRecorded, &NumToWrite, NULL);
SetEndOfFile( FileHandle );
CloseHandle( FileHandle );
FileHandle = INVALID_HANDLE_VALUE;
_debug_print("SaveRecordtoFile SUCCEED!",1);
return 0;
}
示例11: ADIOI_NTFS_Resize
void ADIOI_NTFS_Resize(ADIO_File fd, ADIO_Offset size, int *error_code)
{
LONG dwTemp;
DWORD err;
BOOL result;
static char myname[] = "ADIOI_NTFS_Resize";
dwTemp = DWORDHIGH(size);
err = SetFilePointer(fd->fd_sys, DWORDLOW(size), &dwTemp, FILE_BEGIN);
/* --BEGIN ERROR HANDLING-- */
if (err == INVALID_SET_FILE_POINTER)
{
err = GetLastError();
if (err != NO_ERROR)
{
char errMsg[ADIOI_NTFS_ERR_MSG_MAX];
ADIOI_NTFS_Strerror(err, errMsg, ADIOI_NTFS_ERR_MSG_MAX);
*error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE,
myname, __LINE__, MPI_ERR_IO,
"**io",
"**io %s", errMsg);
return;
}
}
/*printf("setting file length to %d\n", size);fflush(stdout);*/
/* --END ERROR HANDLING-- */
result = SetEndOfFile(fd->fd_sys);
/* --BEGIN ERROR HANDLING-- */
if (result == FALSE)
{
char errMsg[ADIOI_NTFS_ERR_MSG_MAX];
err = GetLastError();
ADIOI_NTFS_Strerror(err, errMsg, ADIOI_NTFS_ERR_MSG_MAX);
*error_code = MPIO_Err_create_code(MPI_SUCCESS, MPIR_ERR_RECOVERABLE,
myname, __LINE__, MPI_ERR_IO,
"**io",
"**io %s", errMsg);
return;
}
/* --END ERROR HANDLING-- */
*error_code = MPI_SUCCESS;
}
示例12: ftruncate
// ---- C.unistd ----
int ftruncate(int fildes, off_t length)
{
HANDLE hfile;
unsigned int curpos;
if( fildes < 0 )
{
errno = EBADF;
return -1;
}
if( length < 0 )
{
errno = EINVAL;
return -1;
}
hfile = (HANDLE) _get_osfhandle (fildes);
curpos = SetFilePointer( hfile, 0, NULL, FILE_CURRENT );
if( (curpos == ~0) ||
(SetFilePointer( hfile, length, NULL, FILE_BEGIN ) == ~0) ||
(!SetEndOfFile( hfile )) )
{
int error = GetLastError();
switch( error )
{
case ERROR_INVALID_HANDLE:
errno = EBADF;
break;
default:
errno = EIO;
break;
}
return -1;
}
return 0;
}
示例13: CreateFile
bool vmsSecurity::ExtractFileFromSignedFile(LPCTSTR ptszSrcFile, LPCTSTR ptszDstFile)
{
HANDLE h = CreateFile (ptszSrcFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
if (h == INVALID_HANDLE_VALUE)
return false;
DWORD dwFileSize = GetFileSize (h, NULL);
DWORD dwDataSize = dwFileSize > 5000 ? 5000 : dwFileSize;
if (dwDataSize < strlen (FdmCryptSignatureMarker))
{
CloseHandle (h);
return false;
}
LPBYTE pbData = new BYTE [dwDataSize];
DWORD dw;
SetFilePointer (h, dwFileSize - dwDataSize, NULL, FILE_BEGIN);
ReadFile (h, pbData, dwDataSize, &dw, NULL);
CloseHandle (h);
int nSigLen = strlen (FdmCryptSignatureMarker);
LPBYTE pbSig = pbData + dwDataSize - nSigLen;
while (pbSig != pbData && strncmp ((char*)pbSig, FdmCryptSignatureMarker, nSigLen) != 0)
pbSig--;
delete [] pbData;
if (pbData == pbSig)
return false;
UINT nSignatureSize = dwDataSize - (pbSig - pbData);
if (FALSE == CopyFile (ptszSrcFile, ptszDstFile, FALSE))
return false;
h = CreateFile (ptszDstFile, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (h == INVALID_HANDLE_VALUE)
return false;
SetFilePointer (h, -(int)nSignatureSize, NULL, FILE_END);
SetEndOfFile (h);
CloseHandle (h);
return true;
}
示例14: MirrorSetAllocationSize
static NTSTATUS DOKAN_CALLBACK
MirrorSetAllocationSize(
LPCWSTR FileName,
LONGLONG AllocSize,
PDOKAN_FILE_INFO DokanFileInfo)
{
WCHAR filePath[MAX_PATH];
HANDLE handle;
LARGE_INTEGER fileSize;
GetFilePath(filePath, MAX_PATH, FileName);
DbgPrint(L"SetAllocationSize %s, %I64d\n", filePath, AllocSize);
handle = (HANDLE)DokanFileInfo->Context;
if (!handle || handle == INVALID_HANDLE_VALUE) {
DbgPrint(L"\tinvalid handle\n\n");
return STATUS_INVALID_HANDLE;
}
if (GetFileSizeEx(handle, &fileSize)) {
if (AllocSize < fileSize.QuadPart) {
fileSize.QuadPart = AllocSize;
if (!SetFilePointerEx(handle, fileSize, NULL, FILE_BEGIN)) {
DWORD error = GetLastError();
DbgPrint(L"\tSetAllocationSize: SetFilePointer eror: %d, "
L"offset = %I64d\n\n", error, AllocSize);
return ToNtStatus(error);
}
if (!SetEndOfFile(handle)) {
DWORD error = GetLastError();
DbgPrint(L"\tSetEndOfFile error code = %d\n\n", error);
return ToNtStatus(error);
}
}
} else {
DWORD error = GetLastError();
DbgPrint(L"\terror code = %d\n\n", error);
return ToNtStatus(error);
}
return STATUS_SUCCESS;
}
示例15: SetFilePointerEx
bool fs::file::trunc(u64 size) const
{
#ifdef _WIN32
LARGE_INTEGER old, pos;
pos.QuadPart = 0;
SetFilePointerEx((HANDLE)m_fd, pos, &old, FILE_CURRENT); // get old position
pos.QuadPart = size;
SetFilePointerEx((HANDLE)m_fd, pos, NULL, FILE_BEGIN); // set new position
SetEndOfFile((HANDLE)m_fd); // change file size
SetFilePointerEx((HANDLE)m_fd, old, NULL, FILE_BEGIN); // restore position
return true; // TODO
#else
return !::ftruncate(m_fd, size);
#endif
}