本文整理汇总了C++中FileHandle::IsValid方法的典型用法代码示例。如果您正苦于以下问题:C++ FileHandle::IsValid方法的具体用法?C++ FileHandle::IsValid怎么用?C++ FileHandle::IsValid使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FileHandle
的用法示例。
在下文中一共展示了FileHandle::IsValid方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: OpenFile
//.........这里部分代码省略.........
m_buffer = 0;
m_eof = true;
}
}
else
{
DWORD s = pFileManager->GetFileSize(m_handle);
DWORD bytesRead = 0;
m_buffer = new char[s + 1];
m_buffer[s] = '\0';
pFileManager->ReadFile(m_handle, m_buffer, s, &bytesRead);
pFileManager->CloseFile(m_handle);
m_size = (size_t)bytesRead;
m_eof = false;
}
}
else
{
m_eof = true;
m_buffer = 0;
}
}
if (!succ && GetDiskFilePriority()<0 && (dwWhereToOpen & FILE_ON_DISK)>0)
{
FileData data = CFileUtils::GetDataFromFile(filename);
if (!data.isNull())
{
m_buffer = data.GetBytes();
m_size = data.GetSize();
data.ReleaseOwnership();
m_eof = FALSE;
succ = TRUE;
}
}
/** extract file to disk for debugging purposes. */
if (m_bExtractFileToDisk && !isEof())
{
string filenameDest = m_strExtractRootPath;
filenameDest.append(filename);
ExtractFileToDisk(filenameDest, false);
}
}
else
{
/** If the file name begins with ':', it is treated as a win32 resource.
e.g.":IDR_FX_OCEANWATER". loads data from a resource of type "TEXTFILE". See MSDN for more information about Windows resources.*/
m_eof = true;
FileData data = CFileUtils::GetResDataFromFile(filename);
if (!data.isNull())
{
m_buffer = data.GetBytes();
m_size = data.GetSize();
data.ReleaseOwnership();
m_bIsOwner = false; // resource file do not need to be deleted on Close().
m_eof = false;
}
}
}
else
{
/// for write-only file, we will save file handle.
m_eof = true; /// set end of file to prevent read access.
if (m_filename == "<memory>")
{
m_bMemoryFile = true;
m_bIsOwner = false;
m_handle.m_pVoidPtr = new StringBuilder();
return true;
}
else
{
FileHandle fileHandle = CFileUtils::OpenFile(filename, false, true);
if (fileHandle.IsValid())
{
m_handle = fileHandle;
m_bDiskFileOpened = true;
return true;
}
else
m_bDiskFileOpened = false;
}
return false;
}
#ifdef FILE_LOGGER_HOOK
if (CFileLogger::GetInstance()->IsBegin())
{
if (!m_eof && !m_filename.empty() && m_filename[0] != ':')
{
CFileLogger::GetInstance()->AddFile(m_filename.c_str(), bReadyOnly);
}
}
#endif
return !m_eof;
}
示例2: SendData
/**
*
*
* @param message
* @param message_size
* @param port
* @param outPrefix [in] - If not null, then every response line will be
* prefixed with this string.
*
* @return bool
*/
static bool SendData(const char *message, size_t message_size, const SockAddr &connectAddr, const char *outPrefix = NULL)
{
size_t totalLength;
vector<char> buffer(max(MaxReplyLineSize, MaxCommandSize));
Socket sendSocket;
FileHandle fileHandle;
uint32_t magic;
if (!sendSocket.OpenTCP(connectAddr.Type()))
{
fprintf(stderr, "Error creating %s socket: %s\n",
Addr::TypeToString(connectAddr.Type()),
SystemErrorToString(sendSocket.GetLastError()));
return false;
}
if (!sendSocket.Connect(connectAddr))
{
fprintf(stderr, "Error connecting to beacon on %s: %s\n",
connectAddr.ToString(),
SystemErrorToString(sendSocket.GetLastError()));
return false;
}
// Assemble the message
magic = htonl(MagicMessageNumber);
memcpy(&buffer.front(), &magic, sizeof(uint32_t));
totalLength = message_size + sizeof(uint32_t);
if (totalLength > MaxCommandSize)
{
fputs("Command too long. Not Sent.\n", stderr);
return false;
}
memcpy(&buffer[sizeof(uint32_t)], message, message_size);
// Send our message.
if (!sendSocket.Send(&buffer.front(), totalLength))
{
fprintf(stderr, "Error sending command to beacon: %s\n", SystemErrorToString(sendSocket.GetLastError()));
return false;
}
// Use stdio for simpler reading of the socket.
fileHandle = fdopen(sendSocket, "r");
if (!fileHandle.IsValid())
{
perror("Error opening socket file: ");
return false;
}
// Read until done
while (fgets(&buffer.front(), buffer.size(), fileHandle))
{
if (outPrefix)
fputs(outPrefix, stdout);
fputs(&buffer.front(), stdout);
}
if (ferror(fileHandle))
{
perror("\nConnection failed. Partial completion may have occurred: \n");
return true;
}
return true;
}