Chinaunix首页 | 论坛 | 博客

  • 博客访问: 474021
  • 博文数量: 86
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 878
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-06 14:11
文章分类

全部博文(86)

文章存档

2010年(12)

2009年(60)

2008年(14)

我的朋友

分类: C/C++

2009-03-02 19:46:45

C 如何得到文件的大小

先用fopen打开文件,然后把文件指针指向文件尾.  
再用ftell获得文件指针当前位置(即文件长度).

源代码:

#include "stdafx.h"
#include
#include

using namespace std;

int main()
{
FILE* fp    = NULL;
int nFileLen = 0;


fp = fopen("c:/Test.txt", "rb");

if (fp == NULL)
{
   cout << "can't open file" << endl;

   return 0;
}


fseek(fp,0,SEEK_END);     //定位到文件末  
nFileLen = ftell(fp);       //文件长度

cout << "file len = " << nFileLen << endl;

return 0;
}

阅读(1801) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~