摘要:本文主要向大家介绍了VC编程获取文件大小,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助。
本文主要向大家介绍了VC编程获取文件大小,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助。
我查到两种方法:
方法一:
WIN32_FIND_DATA fileInfo;
HANDLE hFind;
DWORD fileSize;
const char *fileName = 文件的路径及名字;
hFind = FindFirstFile(fileName ,&fileInfo);
if(hFind != INVALID_HANDLE_VALUE)
fileSize = fileInfo.nFileSizeLow;
FindClose(hFind);
方法二:
HANDLE hFile; // the file handle
DWORD dwFileSize; // temporary storage for file sizes
// Create the test file. Open it "Create Always" to overwrite any
// existing file. The data is re-created below.
hFile = CreateFile(lpcTheFile, GENERIC_READ | GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
printf("hFile is NULL/n");
return 4;
}
dwFileSize = GetFileSize(hFile, NULL);
printf("hFile size: %10d/n", dwFileSize);
C和C++获取文件大小的方法总结
2008年03月31日 星期一 14:21
1.MFC中的方法:(C++)
CFileStatus status;
CFile::GetStatus("D://test.txt",status);
long lSizeOfFile;
lSizeOfFile = status.m_size;
lSizeOfFile的值就是D://test.txt文件的大小
2.标准C获得文件大小的5种方法
(注意:"__FILE__"指的是当前文件,你可以改为有效路径的目标文件,比如"D://test.txt")
#include "stdafx.h"
#include "stdio.h"
#include <sys/stat.h>
#include <io.h>
#include <FCNTL.H>
int getfilesize()
{
int iresult;
struct _stat buf;
iresult = _stat(__FILE__,&buf);
if(iresult == 0)
{
return buf.st_size;
}
return NULL;
}
int getfilesize01()
{
int fp;
fp=_open(__FILE__,_O_RDONLY);
if(fp==-1)
return NULL;
return _filelength(fp);
//return NULL;
}
int getfilesize02()
{
int fp;
fp=_open(__FILE__,_O_RDONLY);
if(fp==-1)
return NULL;
return _lseek(fp,0,SEEK_END);
//return NULL;
}
int getfilesize03()
{
int fp;
fp=_open(__FILE__,_O_RDONLY);
if(fp==-1)
return NULL;
return _lseek(fp,0,SEEK_END);
//return NULL;
}
int getfilesize04()
{
FILE *fp;
if((fp=fopen(__FILE__,"r"))==NULL)
return 0;
fseek(fp,0,SEEK_END);
return ftell(fp); //return NULL;
}
int getfilesize05()
{
FILE *fp;
char str[1];
if((fp=fopen(__FILE__,"rb"))==NULL)
return 0;
for(int i = 0;!feof(fp);i++)
{
fread(&str,1,1,fp);
}
return i - 1; //return NULL;
}
int main(int argc, char* argv[])
{
printf("getfilesize()=%d/n",getfilesize());
printf("getfilesize01()=%d/n",getfilesize01());
printf("getfilesize02()=%d/n",getfilesize02());
printf("getfilesize03()=%d/n",getfilesize03());
printf("getfilesize04()=%d/n",getfilesize04());
printf("getfilesize05()=%d/n",getfilesize05());
return 0;
}
VC获取文件夹大小代码:
以下是代码片段:
DWORD64 GetFolderSize(LPCTSTR szPath, DWORD *dwFiles, DWORD *dwFolders)
{
TCHAR szFileFilter[512];
TCHAR szFilePath[512];
HANDLE hFind = NULL;
WIN32_FIND_DATA fileinfo;
DWORD64 dwSize = 0;
strcpy(szFilePath,szPath);
strcat(szFilePath,"//");
strcpy(szFileFilter,szFilePath);
strcat(szFileFilter,"*.*");
hFind = FindFirstFile(szFileFilter,&fileinfo);
do
{
if(fileinfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (!strcmp(fileinfo.cFileName,".") || !strcmp(fileinfo.cFileName,".."))
{
//Do nothing for "." and ".." folders
}
else
{
TCHAR sztmp[512];
strcpy(sztmp,szFilePath);
strcat(sztmp,fileinfo.cFileName);
dwSize = dwSize + GetFolderSize(sztmp);
if(dwFolders != NULL)
{
++(*dwFolders);
}
}
}
else
{
if(dwFiles != NULL)
{
++(*dwFiles);
}
}
dwSize += fileinfo.nFileSizeLow;
}while(FindNextFile(hFind,&fileinfo));
FindClose(hFind);
return dwSize;
}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言VC/MFC频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号