打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
C++获得文件大小

#include <fstream>

using namespace std;

int main(int argc, char* argv[])

{

ifstream in("file.txt");

in.seekg(0, ios::end); //设置文件指针到文件流的尾部

streampos ps = in.tellg(); //读取文件指针的位置

cout << "File size: " << ps << endl;

in.close(); //关闭文件流

return 0;

}

另一种方法:

对文件操作时有时获得文件的大小时必要的.下面是获得其大小小的较简单方法.
#include<io.h> //C语言头文件
#include<iostream> //for system();
using namespace std;
int main()
{
int handle;
handle = open("test.txt", 0x0100); //open file for read
long length = filelength(handle); //get length of file
cout<<"file length in bytes:"<<length<<endl;
close(handle);

system("pause");
return 0;
}

常用的C语言方法:

将文件打开调整文件指针到文件尾,返回值即位文件大小!
#include <iostream.h>
#include <stdio.h>
void main
{
FILE *fp;
char fileName[100];

cin>>fileName;
fp=fopen(fileName,"r");

long int fileLength=fseek(fp,0L,SEEK_END);
cout<<fileLength;

fclose(fp);
}

类似的方法:

#include <iostream> // not needed for many systems
#include <fstream>
#include <string>

using namespace std;

int main()
{
string inputfilename;
cout << "Enter name for inputfile: ";
cin >> inputfilename;
cout<<endl;
ifstream fin(inputfilename.c_str());

if(!fin.is_open())
{
cout<<"input file open error"<<endl;
return -1;
}

fin.seekg(0,ios::end);

streampos ps = fin.tellg();
cout<<"File size is "<<ps<<endl;
fin.close();

return 0;
}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
读取配置文件函数
C++缺省参数和函数重载
using namespace std详解(收集整理) - mainsun -- IT博客...
命名空间std,using namespace std (转载)
[C/C++]Windows中配置g++编译环境最简单方法
static 全局变量放在头文件中
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服