seekgseekgirl攻略
本文目录一览:
- 1、f.seekg(0, ios::end);是什么意思?
- 2、c++中的seekg函数
- 3、c++中关于文件指针中seekg()的问题
f.seekg(0, ios::end);是什么意思?
seekg是对输入文件定位,它有两个参数:第一个参数是偏移量,第二个参数是基地址。
ios::end是代表流的结尾,0代表偏移量。f.seekg(0, ios::end);意思是直接跳到当前流的结尾,略过流内的所有数据。
1 ::是域操作符。
在C++中,域有两种最常见的基本形式:namespace和class/struct。
::表示在该域内的定义。
属于域定义的一般有两种结构:函数和静态常量/变量。
2 ios是输入输出流的基类。
在数据输出流中,对流定义了很多静态常量。
这里的ios::end是定义的新类型ios::seek_dir(实际定义是int类型)。
c++中的seekg函数
seekg()是对输入流的操作g是get缩写
输入流类的成员函数的名字seekg由两部分组成。首先是seek(寻找)到文件中的某个地方,其次是"g"表示"get",指示函数在输入流上工作,因为要从输入流获取数据。
要查找的文件中的新位置由两个形参给出:新位置将从由place给出的起始位置开始,偏移offset个字节。offset形参是一个long类型的整数,而place可以是ios类中定义的3个值之一。
起始位置可能是文件的开头、文件的当前位置或文件的末尾,这些地方分别由常量ios::beg、ios::cur和ios::end表示。
扩展资料
seekg函数的使用
#includeiostream
#includefstream
usingnamespacestd;
intmain()
{
//Variablesneededtoreadorwritefileonecharacteratatimecharch;
fstreamioFile("rewind.txt",ios::out);
//Openfile.
if(!ioFile)
{
cout"Errorintryingtocreatefile";
return0;
}
//Writetofileandclose
ioFile"Allgooddogs"endl"growl,bark,andeat."endl;
ioFile.close();
//Openthefile
ioFile.open("rewind.txt",ios::in);
if(!ioFile)
{
cout"Errorintryingtoopenfile";
return0;
}
//Readthefileandechotoscreen
ioFile.get(ch);
while(!ioFile.fail())
{
cout.put(ch);
ioFile.get(ch);
}
//Rewindthefile
ioFile.clear();
ioFile.seekg(0,ios::beg);
//Readfileagainandechotoscreen
ioFile.get(ch);
while(!ioFile.fail())
{
cout.put(ch);
ioFile.get(ch);
}
return0;
}
c++中关于文件指针中seekg()的问题
#include iostream
#include fstream
#include assert.h
using namespace std;
int main()
{
ifstream ifs("file1.txt");
char ch;
filebuf* pbuf = ifs.rdbuf();
while(ifs.get(ch)){}
//ifs.seekg(0,ios::beg); //基地址为文件头,偏移量为0,于是定位在文件头
pbuf-pubseekpos (ios_base::beg,ios_base::in);
coutifs.rdbuf(); //从头读出文件内容
return 0;
}
需要修改为pbuf-pubseekpos (ios_base::beg,ios_base::in);,即seekg没有能告诉pbuf移动到文件头了
关于seekg和seekgirl攻略的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
相关文章
发表评论
评论列表
- 这篇文章还没有收到评论,赶紧来抢沙发吧~