关于 ifstream 在 C++ 中的 seekg() 函数的问题
Question about seekg() function of ifstream in C++?
我正在测试以下代码:
int _tmain(int argc, _TCHAR* argv[])
{
int sum = 0;
int x;
ifstream inFile;
inFile.open("test.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
while (inFile >> x) {
cout << x << endl;
}
cout << "-----------------------------" << endl;
// Reading from beggining file again
inFile.seekg(0, ios::beg);
while (inFile >> x) {
cout << x << endl;
}
inFile.close();
return 0;
}
在上面的代码中,我想读取文件,然后将指针移动到文件的开头并再次读取。我已经使用inFile.seekg(0, ios::beg);
回到文件的开头,但它不起作用?请问谁能帮我?谢谢
在你寻找开始之前,你需要清除所有错误标志,否则不会对流执行任何操作:
inFile.clear();
inFile.seekg(0,std::ios::beg);
这是因为将设置eof
位,因为您之前到达了文件的末尾。
我认为您必须通过inFile.clear()重置ifstream的错误标志。否则,它仍然认为它已到达文件末尾。
int autoinc() //auto incriment no//
{
fstream fp;
fp.open("birthreg.dat",ios::in);
fp.seekg(0,ios::beg) ; **//what used this function**
int t=0;
while(fp.read((char*)&st,sizeof(birthreg)))
t=reg_no;
fp.close();
return t;
}
相关文章:
- 类C++中的函数问题(LNK2019和LNK1120错误)
- 返回不停止函数,递归函数问题?(编程练习,动态规划,Levenshtein 回溯)
- 变分模板递归构造函数问题
- C++函数问题中的数组分配
- C++中嵌套在另一个 LinkedList 中的 LinkedList 整数上的回调函数问题
- C++重载构造函数问题
- 二叉搜索树析构函数问题
- C++ vector<pair<int,int>> std::all_of() 函数问题(重定向到头文件)
- 迷宫构造函数问题 [线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)]
- C++函数问题中的 IF 语句
- 函数问题,未定义的引用错误
- C++ 指针无效函数问题
- C++函数问题 = 帮助我理解基本概念
- 由于基于接口的编程,遇到 C++ 虚拟模板函数问题
- C++二进制树递归析构函数问题
- 构造函数问题<无法读取内存>
- 构造函数问题
- C 时间函数问题
- 另一个类中的类的构造函数问题
- C与C++函数问题