KLCFile.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:6k
源码类别:

模拟服务器

开发平台:

C/C++

  1. //////////////////////////////////////////////////////////////////////////////////////
  2. // FileName : CFile.cpp
  3. // FileAuthor : zroc
  4. // FileCreateDate :
  5. // FileDescription : File operation class
  6. //
  7. //////////////////////////////////////////////////////////////////////////////////////
  8. #include <stdafx.h>
  9. #include "KLCFile.h"
  10. //---------------------------------------------------------------------------
  11. //  Function:   Constructor
  12. // Comment: 构造函数
  13. //---------------------------------------------------------------------------
  14. KLCFile::KLCFile()
  15. {
  16. hFile = INVALID_HANDLE_VALUE;
  17. nSize = 0;
  18. dwPos = 0;
  19. }
  20. //---------------------------------------------------------------------------
  21. //  Function:   Destructor
  22. // Comment: 析构函数
  23. //---------------------------------------------------------------------------
  24. KLCFile::~KLCFile()
  25. {
  26. Close();
  27. }
  28. //---------------------------------------------------------------------------
  29. //  Function:   Open exist file
  30. //  Commemt: 打开一个文件,准备读操作
  31. //---------------------------------------------------------------------------
  32. BOOL KLCFile::Open(LPCSTR FileName)
  33. {
  34. char PathName[MAX_PATH];
  35. strcpy(PathName,FileName);
  36. // close prior file handle
  37. if (hFile != INVALID_HANDLE_VALUE)
  38. Close();
  39. // Open the file for read
  40. hFile = CreateFile(
  41. PathName, // pointer to name of the file with path
  42. GENERIC_READ, // access (read-write) mode
  43. FILE_SHARE_READ,// share mode
  44. NULL, // pointer to security attributes
  45. OPEN_EXISTING, // how to create
  46. FILE_ATTRIBUTE_NORMAL,// file attributes
  47. NULL); // template file
  48. // check file handle
  49. if (hFile == INVALID_HANDLE_VALUE)
  50. return FALSE;
  51. // get the open file size
  52. nSize = GetFileSize(hFile, NULL);
  53. // return true
  54. return TRUE;
  55. }
  56. //---------------------------------------------------------------------------
  57. //  Function:   Create new file
  58. //  Comment: 创建一个新文件,准备写操作
  59. //---------------------------------------------------------------------------
  60. BOOL KLCFile::Create(LPCSTR FileName)
  61. {
  62. char PathName[MAX_PATH];
  63. strcpy(PathName,FileName);
  64. // close prior file handle
  65. if (hFile != INVALID_HANDLE_VALUE)
  66. Close();
  67. // create file for write
  68. hFile = CreateFile(
  69. PathName, // pointer to name of the file with path
  70. GENERIC_WRITE, // access (read-write) mode
  71. FILE_SHARE_READ,// share mode
  72. NULL, // pointer to security attributes
  73. CREATE_ALWAYS, // create or over write
  74. FILE_ATTRIBUTE_NORMAL, // file attributes
  75. NULL); // template file
  76. // check file handle
  77. if (hFile == INVALID_HANDLE_VALUE)
  78. return FALSE;
  79. // return true
  80. return TRUE;
  81. }
  82. /*******************************************************
  83. // FUNCTION : CzFileOperate::Eof
  84. // PURPOSE : 
  85. // RETURN : BOOL  TRUE End of File
  86. // COMMENTS :
  87. *******************************************************/
  88. BOOL KLCFile::Eof()
  89. {
  90. if (dwPos >= nSize)
  91. return TRUE;
  92. return FALSE;
  93. }
  94. //---------------------------------------------------------------------------
  95. //  Function:   Close file
  96. //  Comment: 关闭已经打开的文件
  97. //---------------------------------------------------------------------------
  98. BOOL KLCFile::Close()
  99. {
  100. // check file handle
  101. if (hFile == INVALID_HANDLE_VALUE)
  102. return FALSE;
  103. // close file handle
  104. CloseHandle(hFile);
  105. // reset file param
  106. hFile = INVALID_HANDLE_VALUE;
  107. nSize = 0;
  108. dwPos = 0;
  109. // return ture
  110. return TRUE;
  111. }
  112. //---------------------------------------------------------------------------
  113. //  Function:   Read data from file
  114. //  Return: success : number of bytes read from file
  115. //              fail    : 0
  116. //  Comment: 读取文件数据
  117. //---------------------------------------------------------------------------
  118. DWORD KLCFile::Read(LPVOID lpBuffer,DWORD nReadBytes)
  119. {
  120. DWORD BytesRead;
  121. // check file handle
  122. if (hFile == INVALID_HANDLE_VALUE)
  123. return 0;
  124. // read data form file
  125. ReadFile(hFile, lpBuffer, nReadBytes, &BytesRead, NULL);
  126. // move file pointer
  127. dwPos += BytesRead;
  128. // return bytes readed
  129. return BytesRead;
  130. }
  131. //---------------------------------------------------------------------------
  132. //  Function:   Write data to file
  133. //  Return: success : number of bytes write to file
  134. //              fail    : 0
  135. //  Comment: 写入文件数据
  136. //---------------------------------------------------------------------------
  137. DWORD KLCFile::Write(LPVOID lpBuffer,DWORD nWriteBytes)
  138. {
  139. DWORD BytesWrite;
  140. // check file handle
  141. if (hFile == INVALID_HANDLE_VALUE)
  142. return 0;
  143. // write data to file
  144. WriteFile(hFile, lpBuffer, nWriteBytes, &BytesWrite, NULL);
  145. // move file pointer
  146. dwPos += BytesWrite;
  147. // return bytes writed
  148. return BytesWrite;
  149. }
  150. //---------------------------------------------------------------------------
  151. //  Function:   Move file pointer
  152. //  Params: Distance = file offset
  153. //              MoveMethod = FILE_BEGIN
  154. //                           FILE_CURRENT
  155. //                           FILE_END
  156. // Return: success : pointer of file
  157. // fail    : 0xffffffff
  158. //  Comment: 移动文件指针
  159. //---------------------------------------------------------------------------
  160. DWORD KLCFile::Seek(LONG Distance, DWORD MoveMethod)
  161. {
  162. // check file handle
  163. if (hFile == INVALID_HANDLE_VALUE)
  164. return 0xffffffff;
  165. // move file pointer
  166. dwPos = SetFilePointer(hFile, Distance, NULL, MoveMethod);
  167. // return file pointer position
  168. return dwPos;
  169. }
  170. //---------------------------------------------------------------------------
  171. //  Function:   Get file pointer pos
  172. // Return: success : low dword of file size
  173. // fail    : 0xffffffff
  174. //  Comment: 取得文件指针位置
  175. //---------------------------------------------------------------------------
  176. DWORD KLCFile::Tell()
  177. {
  178. // check file handle
  179. if (hFile == INVALID_HANDLE_VALUE)
  180. return 0xffffffff;
  181. // return file size
  182. return dwPos;
  183. }
  184. //---------------------------------------------------------------------------
  185. //  Function:   Get file size (number of bytes)
  186. // Return: success : low dword of file size
  187. // fail    : 0xffffffff
  188. //  Comment: 取得文件的大小
  189. //---------------------------------------------------------------------------
  190. DWORD KLCFile::Size()
  191. {
  192. // check file handle
  193. if (hFile == INVALID_HANDLE_VALUE)
  194. return 0xffffffff;
  195. // return file size
  196. return nSize;
  197. }
  198. //---------------------------------------------------------------------------