请问ifstream怎么获取它所指向的文件的长度?

zxj2006ok 2006-04-27 11:57:46
如题,谢谢
...全文
577 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
howyougen 2006-04-27
  • 打赏
  • 举报
回复
更正一个地方
streampos len== file.tellg();
howyougen 2006-04-27
  • 打赏
  • 举报
回复

int main()
{
ifstream file("filename.txt");
file.seekg(0 , ios::end);
int len = file.tellg();
cout<<len;
system("pause");
return 0;
}
zxj2006ok 2006-04-27
  • 打赏
  • 举报
回复
因为我想根据这个长度去读该文件
dch4890164 2006-04-27
  • 打赏
  • 举报
回复
不用获取文件长度也可以读
#include <iostream.h>
#include <string>
#include <list>
#include <fstream.h>
main()
{
ifstream inputfile("data.txt");
int i = 0;
int count = 0;
while (inputfile>>i)
{
count++;
}
cout<<count;
}

楼主试试,通过上面的程序,建立一个文件,数据不但读入了全部行数,而且还可以判断有多少行.
fiftymetre 2006-04-27
  • 打赏
  • 举报
回复
当使用fstream时,你应当指定ios::in和ios::out作为文件的打开模式

#include <fstream.h>



void main()
{

fstream File("test.txt",ios::in | ios::out);



File << "Hi!"; //将“Hi!”写入文件

static char str[10]; //当使用static时,数组会自动被初始化

//即是被清空为零




File.seekg(ios::beg); // 回到文件首部

// 此函数将在后面解释

File >> str;

cout << str << endl;



File.close();
}



http://blog.cnbie.com/blog_9897.html
逸学堂 2006-04-27
  • 打赏
  • 举报
回复
ifstream in_file1("test.gif",ios::binary); // 打开文件
in_file1.seekg(0,ios_base::end); // 把文件指针到尾部
int iNum = in_file1.tellg(); // 获得文件字节数

// 如果读取文件,需要把文件指针指向文件开始出
in_file1.seekg(0,ios_base::beg);
beginnow 2006-04-27
  • 打赏
  • 举报
回复
去读文件,file.eof() 是否到文件的末尾

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧