写了删改查(增暂时没写),DLL是C++写的。
Cpp源码
#define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING #include <iostream> #include <fstream> #include <string> #include <vector> #include <windows.h> #include <codecvt> #include <locale> //这周末只更新删改查 均用了内存映射 效率很高 编码按要求<a href="http://bbs.125.la/" target="_blank" class="relatedlink">易语言</a>调用的时候改就好 不会出现文件乱码 extern "C" __declspec(dllexport) void replace_line(char* path, int line, char* content) { HANDLE hFile = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { std::cerr << "Could not open file" << std::endl; return; } HANDLE hMapFile = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL); if (hMapFile == NULL) { std::cerr << "Could not create file mapping" << std::endl; CloseHandle(hFile); return; } char* pBuf = (char*)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0); if (pBuf == NULL) { std::cerr << "Could not map view of file" << std::endl; CloseHandle(hMapFile); CloseHandle(hFile); return; } // 检测文本中的换行符是什么 不同的系统版本可能不同 std::string fileContent(pBuf); bool useCRLF = fileContent.find("\r\n") != std::string::npos; int currentLine = 1; char* p = pBuf; char* lineStart = pBuf; while (*p != '\0') { if (currentLine == line) { char* lineEnd = p; while (*lineEnd != '\n' && *lineEnd != '\0') { lineEnd++; } size_t len = lineEnd - lineStart; std::string newContent(content); if (useCRLF) { newContent += "\r"; } size_t newLen = newContent.length(); if (newLen > len) { size_t diff = newLen - len; size_t fileSize = GetFileSize(hFile, NULL); char* newBuf = new char[fileSize + diff]; memcpy(newBuf, pBuf, lineStart - pBuf); memcpy(newBuf + (lineStart - pBuf), newContent.c_str(), newLen); memcpy(newBuf + (lineStart - pBuf) + newLen, lineEnd, fileSize - (lineEnd - pBuf)); UnmapViewOfFile(pBuf); CloseHandle(hMapFile); SetFilePointer(hFile, fileSize + diff, NULL, FILE_BEGIN); SetEndOfFile(hFile); hMapFile = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL); if (hMapFile == NULL) { std::cerr << "Could not create file mapping" << std::endl; CloseHandle(hFile); return; } pBuf = (char*)MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0); if (pBuf == NULL) { std::cerr << "Could not map view of file" << std::endl; CloseHandle(hMapFile); CloseHandle(hFile); return; } memcpy(pBuf, newBuf, fileSize + diff); delete[] newBuf; } else { memcpy(lineStart, newContent.c_str(), newLen); memmove(lineStart + newLen, lineEnd, strlen(lineEnd) + 1); } break; } if (*p == '\n') { currentLine++; lineStart = p + 1; } p++; } UnmapViewOfFile(pBuf); CloseHandle(hMapFile); CloseHandle(hFile); } extern "C" __declspec(dllexport) void del_line(char* path, int line) { HANDLE hFile = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { std::cerr << "Could not open file." << std::endl; return; } DWORD dwFileSize = GetFileSize(hFile, NULL); if (dwFileSize == INVALID_FILE_SIZE) { std::cerr << "Could not get file size." << std::endl; CloseHandle(hFile); return; } HANDLE hMapFile = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, dwFileSize, NULL); if (hMapFile == NULL) { std::cerr << "Could not create file mapping object." << std::endl; CloseHandle(hFile); return; } LPVOID lpMapAddress = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, dwFileSize); if (lpMapAddress == NULL) { std::cerr << "Could not map view of file." << std::endl; CloseHandle(hMapFile); CloseHandle(hFile); return; } char* data = (char*)lpMapAddress; int currentLine = 1; DWORD startOffset = 0; for (DWORD i = 0; i < dwFileSize; i++) { if (data<i> == '\n') { currentLine++; if (currentLine == line) { startOffset = i + 1; break; } } } DWORD endOffset = startOffset; for (DWORD i = startOffset; i < dwFileSize; i++) { if (data<i> == '\n') { endOffset = i + 1; break; } } MoveMemory(data + startOffset, data + endOffset, dwFileSize - endOffset); UnmapViewOfFile(lpMapAddress); CloseHandle(hMapFile); SetFilePointer(hFile, dwFileSize - (endOffset - startOffset), NULL, FILE_BEGIN); SetEndOfFile(hFile); CloseHandle(hFile); } extern "C" __declspec(dllexport) char* get_line(char* path, int line) { HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) return NULL; HANDLE hMap = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL); if (hMap == NULL) { CloseHandle(hFile); return NULL; } char* pFile = (char*)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0); if (pFile == NULL) { CloseHandle(hMap); CloseHandle(hFile); return NULL; } int count = 0; char* pLine = pFile; char* pEnd = pLine; while (*pEnd != '\0') { if (*pEnd == '\n' || (*pEnd == '\r' && *(pEnd + 1) == '\n')) { count++; if (count == line) break; pLine = pEnd + 1 + (*pEnd == '\r'); pEnd += (*pEnd == '\r'); } pEnd++; } if (count != line) { UnmapViewOfFile(pFile); CloseHandle(hMap); CloseHandle(hFile); return NULL; } int len = pEnd - pLine; char* result = new char[len + 1]; memcpy(result, pLine, len); result[len] = '\0'; UnmapViewOfFile(pFile); CloseHandle(hMap); CloseHandle(hFile); return result; }</i></i> <i><i>extern "C" __declspec(dllexport) char* read_text(char* path) { HANDLE hFile = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); if (hFile == INVALID_HANDLE_VALUE) return nullptr; DWORD fileSize = GetFileSize(hFile, nullptr); HANDLE hMapFile = CreateFileMapping(hFile, nullptr, PAGE_READONLY, 0, fileSize, nullptr); if (hMapFile == nullptr) { CloseHandle(hFile); return nullptr; } char* buffer = (char*)MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, fileSize); if (buffer == nullptr) { CloseHandle(hMapFile); CloseHandle(hFile); return nullptr; } int isTextUnicode = IS_TEXT_UNICODE_UNICODE_MASK; IsTextUnicode(buffer, fileSize, &isTextUnicode); char* gbkBuffer; if (isTextUnicode == IS_TEXT_UNICODE_UNICODE_MASK) { std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter; std::wstring wide = converter.from_bytes(buffer, buffer + fileSize); int gbkSize = WideCharToMultiByte(CP_ACP, 0, wide.c_str(), -1, nullptr, 0, nullptr, nullptr); gbkBuffer = new char[gbkSize]; WideCharToMultiByte(CP_ACP, 0, wide.c_str(), -1, gbkBuffer, gbkSize, nullptr, nullptr); } else { gbkBuffer = new char[fileSize + 1]; memcpy(gbkBuffer, buffer, fileSize); gbkBuffer[fileSize] = '\0'; } UnmapViewOfFile(buffer); CloseHandle(hMapFile); CloseHandle(hFile); return gbkBuffer; } </i></i>
隐藏内容需要回复可以看见
回复
卧槽!无奈本人没文化,一句卧槽行天下!
楼主辛苦了,谢谢楼主,楼主好人一生平安!
楼主辛苦了,谢谢楼主,楼主好人一生平安!