惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Schneier on Security
V
Visual Studio Blog
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
Y
Y Combinator Blog
N
Netflix TechBlog - Medium
Recent Announcements
Recent Announcements
U
Unit 42
D
Docker
Recorded Future
Recorded Future
GbyAI
GbyAI
C
Check Point Blog
博客园 - 叶小钗
大猫的无限游戏
大猫的无限游戏
博客园 - 司徒正美
月光博客
月光博客
A
About on SuperTechFans
Last Week in AI
Last Week in AI
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
F
Fortinet All Blogs
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Jina AI
Jina AI
G
GRAHAM CLULEY
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
I
InfoQ
AWS News Blog
AWS News Blog
The Hacker News
The Hacker News
Cyberwarzone
Cyberwarzone
博客园 - 三生石上(FineUI控件)
P
Palo Alto Networks Blog
C
CERT Recently Published Vulnerability Notes
aimingoo的专栏
aimingoo的专栏
S
Securelist
F
Full Disclosure
T
The Exploit Database - CXSecurity.com
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
T
Tor Project blog
Scott Helme
Scott Helme
T
Threat Research - Cisco Blogs
NISL@THU
NISL@THU
A
Arctic Wolf
美团技术团队
G
Google Developers Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LangChain Blog
Simon Willison's Weblog
Simon Willison's Weblog
Apple Machine Learning Research
Apple Machine Learning Research

博客园 - 好好学习,天天进步

Java调用ocx控件以及dll pongo英雄会-修路题解(z) 那些VisualStudio隐藏的调试功能(转) Android 横屏切换竖屏Activity的生命周期(转) 那些有争议的编程观点 经过完整测试的农历-公历相互转换 windows phone SDK 8.0 模拟器异常 0x89721800解决办法 JavaScript 解析器相关资料 AST Parser。。。 (转)浏览器的工作原理:新式网络浏览器幕后揭秘 中缀表达式转后缀表达式(逆波兰表达式) Windows 8 无法安装 Algorithm Gossip: 中序式轉後序式(前序式) tesseract-ocr训练方法 透明窗口与不规则窗口制作方法总结 检测文件存在的四种方法 从浏览器启动客户端程序 Struts 2命令执行漏洞 win7 x64怎么枚举所有快捷键呢 在windbg中测试shadow ssdt , win32k!NtUserGetForegroundWindow , hook shadow ssdt
C/C++多种方法获取文件大小
好好学习,天天进步 · 2012-08-07 · via 博客园 - 好好学习,天天进步

http://www.cnblogs.com/cxun/archive/2009/02/24/1397153.html

#include <iostream>
#include 
<io.h>
#include 
<sys\stat.h>
#include 
<afx.h>
#define _AFXDLL
using namespace std;void main()
{
    
// 此文件在工程打开状态下为不可访问
    char* filepath = "..\\test.ncb";// 方法一
    struct _stat info;
    _stat(filepath, 
&info);
    
int size = info.st_size;
    cout
<<size<<endl;// 方法二
    FILE* file = fopen(filepath, "rb");
    
if (file)
    {
        
int size = filelength(fileno(file));
        cout
<<size<<endl;
        fclose(file);
    }
// 方法三
    CFile cfile;
    
if (cfile.Open(filepath, CFile::modeRead))
    {
        
int size = cfile.GetLength();
        cout
<<size<<endl;
    }
// 方法四
    HANDLE handle = CreateFile(filepath, FILE_READ_EA, FILE_SHARE_READ, 0, OPEN_EXISTING, 00);
    
if (handle != INVALID_HANDLE_VALUE)
    {
        
int size = GetFileSize(handle, NULL);
        cout
<<size<<endl;
        CloseHandle(handle);
    }
}

VS2005:若编译链接不通过,需要修改工程设置:

(1) Configuration Properties -> C/C++ -> Code Generation -> Runtime Library, 选择"Multi-threaded Debug(/MTd)"

(2) Configuration Properties -> Linker -> Input -> Ignore Specific Library, 输入"msvcprtd.lib"

例子中,由于在工程打开状态下,test.ncb文件无法打开,所以方法二、三无法获取文件大小,因此推荐使用方法一、四。