天天看點

一次練習 發現的問題,malloc free 無效;findfirstfile失敗,writefile失敗的原因

#include <windows.h>
#include <stdio.h>
#include <shlwapi.h>
 
#pragma comment(lib,"shlwapi.lib")
 
int myRemoveDirectory(char *szPath)
{
    if(!PathIsDirectory(szPath))
    {
        return GetLastError();
    }
    BOOL bRes = SetCurrentDirectory(szPath);
    if(!bRes)
    {
        return GetLastError();
    }
    char * buf =(char *)malloc(MAX_PATH);
    ZeroMemory(buf,sizeof(buf));
 
    strcpy(buf,szPath);
 
    PathAddBackslash(buf);
 
    strcat(buf,"*.*");
    
    WIN32_FIND_DATA findData = {0};
    HANDLE hFile = FindFirstFile(buf,&findData);
    int nRes = DeleteFile(findData.cFileName);
    while(FindNextFile(hFile,&findData))
    {
        DeleteFile(findData.cFileName);
    }
    SetCurrentDirectory("D:\\");
    FindClose(hFile);
    if(!RemoveDirectory(szPath))
    {
        int  nRes = GetLastError();
        return  nRes;
    }
    return 0;
}
 
char *  MyCreateFile()
{
    HANDLE hFile = NULL;
    char * cPath = "d:\\temp________111111111111";
    if(!myRemoveDirectory(cPath))
    {
        
    }
    BOOL bRes = CreateDirectory(cPath,NULL);
    int nRes = GetLastError();
    if(!bRes)
    {
        MessageBox(NULL,"Failed to create files","Alert",MB_OK);
        return    NULL;
    }
    bRes = SetCurrentDirectory(cPath);
    char * pszFilePath = (char *)malloc(1024);
    strcpy(pszFilePath,cPath);
    PathAppend(pszFilePath,"text.txt");
    hFile = CreateFile(pszFilePath,GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    if(hFile == NULL)
    {
        return NULL;
    }
    int i,j;
    char *buff = (char *)malloc(1024);
    char * mallocHeap = buff; // 記錄新建立的位址。
    if(hFile == INVALID_HANDLE_VALUE)
    {
        return NULL; 
    }
    DWORD nBytes = 0;
    char temp[MAX_PATH]={0};
    ZeroMemory(buff,1024);
    for(i=1;i<10;i++)
    {
        for(j=1;j<=i;j++)
        {            
            sprintf(temp,"%d*%d = %d \t",j,i,i*j);    //改變temp的指向,temp 不在指向原先的記憶體位址。 
            strcat(buff,temp);
        }
        strcat(buff,"\r\n");
        WriteFile(hFile,buff,strlen(buff)+1,&nBytes,NULL);
        ZeroMemory(buff,1024);
    }
    
    CloseHandle(hFile);
    free(mallocHeap); 
 
    return pszFilePath;
    
}
int MyReadFile()
{
    char * pszFilePath = MyCreateFile();
 
    HANDLE hFile = CreateFile(pszFilePath,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    if(hFile == NULL)
    {
        return 0;
    }
    char *buf = (char * )malloc(1024*1024);
    char * mallocHeap = buf;
    ZeroMemory(buf,sizeof(buf));
    DWORD nBytes = 0;
    ReadFile(hFile,buf,1024,&nBytes,NULL);
    if(nBytes < 1024 )
    {
        while(nBytes)
        {
            printf("%s",buf);
            int nTemp = strlen(buf);
            buf = buf + nTemp +1;
            nBytes = nBytes- nTemp-1;
        }
    }
    else
    {
        while(nBytes)
        {
 
            printf("%s",buf);
            ZeroMemory(buf,sizeof(buf));
            ReadFile(hFile,buf,1024,&nBytes,NULL);
        }
 
    }
    CloseHandle(hFile);
    free(mallocHeap);
    return 1;
}
int main()
{
    if(MyReadFile())
    {
        MessageBox(NULL,"Success","Success",MB_OK);
    }
    getchar();
    getchar();
    return 0;
}
      

  

總結:

free 隻能釋放malloc alloc 建立的堆指針,改變無效。

findfirstfile 實在一對檔案中尋找檔案,而不是在一個檔案夾裡尋找檔案。

也就是說 findfirstfile(d:\\*.*) 是可以找到目錄下面的檔案的。

大師 findfirstfile(d:\\) 不行。

wirtefile 當第四個參數為null的時候 第三個參數不允許為null