文件倒序示例应用程序关键代码(使用内存映射文件)——摘自Windows 核心编程
来源:互联网 发布:编程培训课程 编辑:程序博客网 时间:2024/06/15 11:52
BOOL FileReverse(PCTSTR pszPathname, PBOOL pfIsTextUnicode) {
*pfIsTextUnicode = FALSE; // Assume text is Unicode
// Open the file for reading and writing.
HANDLE hFile = CreateFile(pszPathname, GENERIC_WRITE | GENERIC_READ, 0,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
chMB("File could not be opened.");
return(FALSE);
}
// Get the size of the file (I assume the whole file can be mapped).
DWORD dwFileSize = GetFileSize(hFile, NULL);
// Create the file-mapping object. The file-mapping object is 1 character
// bigger than the file size so that a zero character can be placed at the
// end of the file to terminate the string (file). Because I don't yet know
// if the file contains ANSI or Unicode characters, I assume worst case
// and add the size of a WCHAR instead of CHAR.
HANDLE hFileMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE,
0, dwFileSize + sizeof(WCHAR), NULL);
if (hFileMap == NULL) {
chMB("File map could not be opened.");
CloseHandle(hFile);
return(FALSE);
}
// Get the address where the first byte of the file is mapped into memory.
PVOID pvFile = MapViewOfFile(hFileMap, FILE_MAP_WRITE, 0, 0, 0);
if (pvFile == NULL) {
chMB("Could not map view of file.");
CloseHandle(hFileMap);
CloseHandle(hFile);
return(FALSE);
}
// Does the buffer contain ANSI or Unicode?
int iUnicodeTestFlags = -1; // Try all tests
*pfIsTextUnicode = IsTextUnicode(pvFile, dwFileSize, &iUnicodeTestFlags);
if (!*pfIsTextUnicode) {
// For all the file manipulations below, we explicitly use ANSI
// functions because we are processing an ANSI file.
// Put a zero character at the very end of the file.
PSTR pchANSI = (PSTR) pvFile;
pchANSI[dwFileSize / sizeof(CHAR)] = 0;
// Reverse the contents of the file.
_strrev(pchANSI);
// Convert all "/n/r" combinations back to "/r/n" to
// preserve the normal end-of-line sequence.
pchANSI = strchr(pchANSI, '/n'); // Find first '/n'.
while (pchANSI != NULL) {
// We have found an occurrence....
*pchANSI++ = '/r'; // Change '/n' to '/r'.
*pchANSI++ = '/n'; // Change '/r' to '/n'.
pchANSI = strchr(pchANSI, '/n'); // Find the next occurrence.
}
} else {
// For all the file manipulations below, we explicitly use Unicode
// functions because we are processing a Unicode file.
// Put a zero character at the very end of the file.
PWSTR pchUnicode = (PWSTR) pvFile;
pchUnicode[dwFileSize / sizeof(WCHAR)] = 0;
if ((iUnicodeTestFlags & IS_TEXT_UNICODE_SIGNATURE) != 0) {
// If the first character is the Unicode BOM (byte-order-mark),
// 0xFEFF, keep this character at the beginning of the file.
pchUnicode++;
}
// Reverse the contents of the file.
_wcsrev(pchUnicode);
// Convert all "/n/r" combinations back to "/r/n" to
// preserve the normal end-of-line sequence.
pchUnicode = wcschr(pchUnicode, L'/n'); // Find first '/n'.
while (pchUnicode != NULL) {
// We have found an occurrence....
*pchUnicode++ = L'/r'; // Change '/n' to '/r'.
*pchUnicode++ = L'/n'; // Change '/r' to '/n'.
pchUnicode = wcschr(pchUnicode, L'/n'); // Find the next occurrence.
}
}
// Clean up everything before exiting.
UnmapViewOfFile(pvFile);
CloseHandle(hFileMap);
// Remove trailing zero character added earlier.
SetFilePointer(hFile, dwFileSize, NULL, FILE_BEGIN);
SetEndOfFile(hFile);
CloseHandle(hFile);
return(TRUE);
}
- 文件倒序示例应用程序关键代码(使用内存映射文件)——摘自Windows 核心编程
- 《Windows核心编程》读书心得——内存(内存映射文件)(5)
- Windows核心编程:内存映射文件
- 《Windows核心编程》---内存映射文件基础
- Windows核心编程:内存映射文件
- windows核心编程-内存映射文件(一)
- windows核心编程-内存映射文件(二)
- Windows 核心编程 17 内存映射文件
- 《Windows核心编程》之“内存映射文件”
- windows核心编程---内存映射文件
- Windows核心编程(十六)内存映射文件
- Windows核心编程 第十七章 -内存映射文件(上)
- Windows核心编程 第十七章 -内存映射文件(下)
- Windows核心编程笔记(十六)内存映射文件
- window 核心编程 (内存映射文件)
- 内存映射文件——Windows核心编程学习手札之十七
- Windows核心编程——》第十七章 内存映射文件 (Memory-Mapped Files)
- windows核心编程--内存映射文件
- 类和对象的定义
- 《架构实践--软件架构设计的过程》读书笔记
- 二、ANDROID命令详解
- Android SDK 2.1 - Dev Guide - Best Practives - UI Guidelines - Activity and Task Design - 中文/Chinese
- Nutch 源码分析
- 文件倒序示例应用程序关键代码(使用内存映射文件)——摘自Windows 核心编程
- Eclipse中快速重写(Override)基类方法的技巧
- 技术人才的几种划分
- LVS
- C++继承构造函数调用顺序问题
- 解决MyEclipse ALT+/没有自动提示
- MyEclipse 'com.microsoft.sqlserver.jdbc.SQLServerDriver' Not Found
- const static与类的数据成员,成员函数,对象
- DBMS_STATS.GATHER_TABLE_STATS 和 analyze