天下

记录修行的印记

VC判断目录是否存在及遍历目录指定的文件.

// MakePhotoLog.cpp : Defines the entry point for the console application.

#include 
"stdafx.h"

#pragma pack(
1)
typedef 
struct __tag_file_info
{
    DWORD    length;    
//文件长度
    DWORD    crc32;    //暂未使用
    char    name[38];//文件名
    char    res[4];     
} file_info;            
//Total = 50
#pragma pack()

long GetLocalFileSize(const char* szFileName)
{
    
struct stat f_stat;
    
if (stat(szFileName, &f_stat) == -1) {
        
return -1;
    }
    
return (long)f_stat.st_size;
}

BOOL IsDirectory(CString strPath) 
{     
    WIN32_FIND_DATA wfd;     
    HANDLE hFind 
= FindFirstFile(strPath, &wfd);     
    BOOL bRet 
= ((hFind!=INVALID_HANDLE_VALUE) &&  (wfd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY));
    FindClose(hFind);     
    
return bRet; 
}



int main(int argc, char *argv[])
{
    WIN32_FIND_DATA wfd;
    HANDLE hFind;
    CString strPath;
    CString strFileName;
    CString strFind;
    CString strJpgFile;

    
if (argc<2)
    {
        printf(
"Usage:%s <photo-log path>\r\n",argv[0]);
        
return -1;
    }

    strPath 
= argv[1];
    
if (!IsDirectory(strPath))
    {
        printf(
"\"%s\" not found\r\n",argv[1]);
        
return -1;
    }
    
    PathAddBackslash(strPath.GetBuffer(strPath.GetLength()
+2));
    strPath.ReleaseBuffer();


    strFileName     
= strPath;
    strFileName 
+= "photo_log.dat";
    
    FILE
* fp = fopen("photo_log.dat","w+b");
    
if (fp==NULL)
    {
        printf(
"open \"%s\" error \r\n",(LPCTSTR)strFileName);
        
return -1;
    }

    strFind 
= strPath + "*.jpg";
    hFind 
= FindFirstFile(strFind, &wfd);
    
if (hFind == INVALID_HANDLE_VALUE) 
    {
        printf (
"Invalid File Handle. GetLastError reports %d\n", GetLastError ());
        
return -1;
    }
    
    BOOL bRet 
= TRUE;
    file_info fi;
    
int nSucced = 0;
    
while(bRet)
    {
        strJpgFile 
= strPath + wfd.cFileName;
        
if (strlen(wfd.cFileName)>32)
        {
            
continue;
        }
        memset(
&fi,0,sizeof(fi));
        fi.length 
= wfd.nFileSizeLow;
        strcpy(fi.name,wfd.cFileName);

        
if (fwrite(&fi,1,sizeof(fi),fp) == sizeof(fi))
        {
            nSucced
++;
            printf(
"%d. make photo-log(%s) succed\r\n", nSucced,(LPCTSTR)strJpgFile );
        }
        
else{
            printf(
"make photo-log(%s) failed\r\n", (LPCTSTR)strJpgFile );
            
break;
        }

        bRet 
= FindNextFile(hFind,&wfd);
    }
    printf(
"make photo-log(%d) succed\r\n",nSucced);
    FindClose(hFind);
    
if (fp!=NULL)
    {
        fclose(fp);
    }
    
return 0;
}



posted on 2013-01-10 18:22 天下 阅读(699) 评论(0)  编辑 收藏 引用 所属分类: C/C++Win32


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2012年8月>
2930311234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(4)

随笔分类(378)

随笔档案(329)

链接

最新随笔

搜索

最新评论