- #include<iostream.h>//C++显示(读取)文件大小源程序
- #include<fstream.h>
- void main()
- {
- char fname[20];
- cout<<"输入文件名:";
- cin>>fname;
- ifstream rs(fname,ios::nocreate|ios::in);
- try
- {
- if(rs.fail())
- //当文件不存在时,rs.fail()返回真
- throw "error!";
- }
- catch(char *s)
- {
- cout<<s<<fname<<"文件不存在!"<<endl;
- return;
- }
- rs.seekg(0,ios::end);
- //此句意为将文件的读指针移至文件末尾
- cout<<fname<<"文件大小:"<<rs.tellg()<<"Bytes"<<endl;
- //tellg()函数返回当前文件指针的位置
- }
- //该片段来自于http://www.codesnippet.cn/detail/04122012796.html
来源: http://www.codesnippet.cn/detail/04122012796.html