这次希望你有满意的答案
#include <windows.h>
void RemoveEnter(char *pFilePath);
void main()
{
RemoveEnter("d:\\abc.txt");
}
void RemoveEnter(char *pFilePath)
{
HANDLE hFile = NULL;
char *pFileRead
= NULL;
char *pFileWrite = NULL;
DWORD dwFileSize = 0;
DWORD dwFileReadWrite = 0;
int nIndexRead = 0;
int nIndexWrite = 0;
// 以读的方式打开文件
hFile = ::CreateFileA(pFilePath, GENERIC_READ,FILE_SHARE_WRITE,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hFile) {
return;
}
// 获取文件大小,
dwFileSize = ::GetFileSize(hFile, NULL);
if (dwFileSize == 0) {
goto _EXIT;
}
// 申请读写 缓存
pFileRead = (char *)malloc(dwFileSize);
pFileWrite = (char *)malloc(dwFileSize);
if (pFileRead == NULL || pFileWrite == NULL) {
goto _EXIT;
}
memset(pFileRead, 0, dwFileSize);
memset(pFileWrite, 0, dwFileSize);
::ReadFile(hFile, pFileRead, dwFileSize, &dwFileReadWrite, NULL);
if (dwFileSize != dwFileReadWrite) {
goto _EXIT;
}
while (nIndexRead < dwFileSize) { // 不是换行的,保持到内存
if (pFileRead[nIndexRead] != '\r' && pFileRead[nIndexRead] != '\n') {
pFileWrite[nIndexWrite] = pFileRead[nIndexRead];
nIndexWrite++;
}
nIndexRead++;
}
// 关闭原来的文件。
::CloseHandle(hFile);
// 再次打开,这次是写入文件
CreateAlways 覆盖写入
hFile = ::CreateFileA(pFilePath, GENERIC_WRITE,FILE_SHARE_WRITE,NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hFile) {
goto _EXIT;
}
::WriteFile(hFile, pFileWrite, nIndexWrite, &dwFileReadWrite, NULL);
_EXIT:
if (INVALID_HANDLE_VALUE != hFile) {
::CloseHandle(hFile);
hFile = NULL;
}
if (pFileRead != NULL) {
free(pFileRead);
pFileRead = NULL;
}
if (pFileWrite != NULL) {
free(pFileWrite);
pFileWrite = NULL;
}
}