zoukankan      html  css  js  c++  java
  • 一次练习 发现的问题,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 	",j,i,i*j);    //改变temp的指向,temp 不在指向原先的内存地址。 
                strcat(buff,temp);
            }
            strcat(buff,"
    ");
            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


  • 相关阅读:
    Google调试技巧总结
    Reorder List -- leetcode
    Java回合阵列List
    他们控制的定义(2.3):SurfaceView和SurfaceHolder
    ImageView建立selector在录音中遇到的小问题及解决方案
    cocos2d 简单的日常高仿酷跑游戏
    Xcode的小标记旁边的文件的名称的作用
    c++中的对象引用(object reference)与对象指针的区别
    UIColor深入研究(CGColor,CIColor)
    UIImage图片处理
  • 原文地址:https://www.cnblogs.com/S-volcano/p/3387751.html
Copyright © 2011-2022 走看看