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函数超出范围问题
C++,seekg用在读取文件的时候,seekg(offset,dir),offset是一个位移值,为Long型,=0是当前,0向后移,0则向前移。dir有3个参数,ios::beg文件开头,ios::end文件结尾,ios::cur当前指针位置.你读取文件后,注意读的时候是ios::in,写是ios::out你在用seekg(10L,beg),表示文件开头向后移10个字节,在用couttellg()显示位移后的位置。
用C++写文件,seekg函数为什么无法显示完整数据?变成了00?
//file.seekg(i*sizeof(struct student_type),ios::beg);
这行屏蔽掉,就可以了。
c++文件部分关于seekg的问题。
istream::seekg 从来就没有省略参数
istream seekg( streampos pos ); 指定一个 相对文件流开头的绝对位置
istream seekg( streamoff off, ios::seek_dir dir );指定一个相对 位置
Parameters 参数
pos
The new position value; streampos is a typedef equivalent to long.
新的位置,就是个long值
off
The new offset value; streamoff is a typedef equivalent to long.
偏移量(相对dir的位置),是个long值
dir
The seek direction. Must be one of the following enumerators:
位置, 必须是以下之一
ios::beg Seek from the beginning of the stream.
开头
ios::cur Seek from the current position in the stream.
当前位置
ios::end Seek from the end of the stream.
末尾
请问如果seekg与seekp没什么分别,那它们分别存在的意思是什么。
1) 你用的Linux XXX版本的libstdc++有可能使用同一個指針去實現seekg和seekp的功能,但在標準中你不該理解為同一的指針. 所以你理論上不用管他們是否IO都用同一個指針而是看有沒有錯。
但問題不應該在互相影響上,而是在你沒有使用tellg時你不能說seekg的位置對不對, 同時你不用tellp不被調時,你也不可能知道seekp對不對,重點是你用seekp前要先知道自己在那,除了頭和尾.
3) 一個是用來讀的interface另一個是用來寫的, 如果你再往深一層去想,把File Stream的操作改為通用的stream,那不同的stream就有可能有不一樣的g和p指針而你的實現將不能重用.
文件读到文末seekg()函数无效
哈,网站上的,你看看
"streampos tellg();
Get position in input sequence
Returns the position of the current character in the input stream.
if member fail returns true, the function returns -1.
"
因为你之前把唯一的一行文件内容读完了,也就是出现了eof,所以fail()函数返回true,所以seekg返回-1.在seekg()函数之前调用clear()就好了。
另外在最新的C++版本里,seekg函数会自己先调用clear,也就是说你直接这样写也许会是对的哦。
关于seekg和seekgirlfog1玉符怎么用的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。