大佬教程收集整理的这篇文章主要介绍了C++ seekg函数用法详解,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
seekg(offset,placE);
这个输入流类的成员函数的名字 seekg 由两部分组成。首先是 seek(寻找)到文件中的某个地方,其次是 "g" 表示 "get",指示函数在输入流上工作,因为要从输入流获取数据。seekg(0L,ios::beg);
以上语句表示从文件的开头位置开始,移动 0 字节,实际上就是指移动到文件开头。
dataIn.clear();
dataIn.seekg(0L,ios::beg);
//Program shows how to rewind a file. It writes a text file and opens it for reading,then rewinds // it to the beginning and reads it again. #include <iostream> #include <fstream> using namespace std; int main() { // Variables needed to read or write file one character at a time char ch; fstream ioFile("rewind.txt",ios::out); // Open file. if (!ioFilE) { cout << "Error in trying to create file"; return 0; } // Write to file and close ioFile << "All good dogs" << endl << "growl,bark,and eat." << endl; ioFile.close(); //Open the file ioFile.open ("rewind.txt",ios::in); if (!ioFilE) { cout << "Error in trying to open file"; return 0; } // Read the file and echo to screen ioFile.get(ch); while (!ioFile.fail()) { cout.put(ch); ioFile.get(ch); } //Rewind the file ioFile.clear(); ioFile.seekg(0,ios::beg); //Read file again and echo to screen ioFile.get(ch); while (!ioFile.fail()) { cout.put(ch); ioFile.get(ch); } return 0; }程序输出结果:
All good dogs
growl,and eat.
All good dogs
growl,and eat.
以上是大佬教程为你收集整理的C++ seekg函数用法详解全部内容,希望文章能够帮你解决C++ seekg函数用法详解所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。