今天下午遇到这样的一个问题: 逐行读取了test.txt文件后, 后续需要继续从头开始重新逐行读取, 用C++怎么做呢?
下面, 我们先在工程当前目录下制作我们需要的test.txt文件, 在里面输入几行字符串:
ab
cd
ef
gh
我们先看逐行读取文件:
#include
#include
#include
using namespace std;
int main()
{
ifstream in("test.txt");
string line;
// 下面代码有效
if(in)
{
while(getline(in, line))
{
cout << line.c_str() << endl;
}
}
return 0;
}
#include
#include
#include
using namespace std;
int main()
{
ifstream in("test.txt");
string line;
// 下面代码有效
if(in)
{
while(getline(in, line))
{
cout << line.c_str() << endl;
}
}
// 不会进入下面的if
if(in)
{
cout << "test" << endl;
while(getline(in, line))
{
cout << line.c_str() << endl;
}
}
return 0;
}
#include
#include
#include
using namespace std;
int main()
{
ifstream in("test.txt");
string line;
// 下面代码有效
if(in)
{
while(getline(in, line))
{
cout << line.c_str() << endl;
}
}
in.seekg(0, ios::beg); // 指向begin处
// 仍然不会进入下面的if
if(in)
{
cout << "test" << endl;
while(getline(in, line))
{
cout << line.c_str() << endl;
}
}
return 0;
}
#include
#include
#include
using namespace std;
int main()
{
ifstream in("test.txt");
string line;
// 下面代码有效
if(in)
{
while(getline(in, line))
{
cout << line.c_str() << endl;
}
}
in.close();
in.open("test.txt");
// 仍然不会进入下面的if
if(in)
{
cout << "test" << endl;
while(getline(in, line))
{
cout << line.c_str() << endl;
}
}
return 0;
}
还是不行, 难道close, open之后还需要seekg? 看:
#include
#include
#include
using namespace std;
int main()
{
ifstream in("test.txt");
string line;
// 下面代码有效
if(in)
{
while(getline(in, line))
{
cout << line.c_str() << endl;
}
}
in.close();
in.open("test.txt");
in.seekg(0, ios::beg); // 指向begin处
// 仍然不会进入下面的if
if(in)
{
cout << "test" << endl;
while(getline(in, line))
{
cout << line.c_str() << endl;
}
}
return 0;
}
看程序:
#include
#include
#include
using namespace std;
int main()
{
ifstream in("test.txt");
string line;
// 下面代码有效
if(in)
{
while(getline(in, line))
{
cout << line.c_str() << endl;
}
}
if(in.eof())
{
cout << "end of file" << endl; // 进入到了这里
}
else
{
cout << "in the file" << endl;
}
return 0;
}
#include
#include
#include
using namespace std;
int main()
{
ifstream in("test.txt");
string line;
// 下面代码有效
if(in)
{
while(getline(in, line))
{
cout << line.c_str() << endl;
}
}
in.clear();
if(in.eof())
{
cout << "end of file" << endl;
}
else
{
cout << "in the file" << endl; // 进入到了这里
}
return 0;
}
恩, clear功能真大啊, 好, 我们顺着这个思路继续看:
#include
#include
#include
using namespace std;
int main()
{
ifstream in("test.txt");
string line;
// 下面代码有效
if(in)
{
while(getline(in, line))
{
cout << line.c_str() << endl;
}
}
in.clear();
// 会进入下面的if, 但不会进入while
if(in)
{
cout << "test" << endl;
while(getline(in, line))
{
cout << line.c_str() << endl;
}
}
return 0;
}
恩, 貌似还差点什么吧, 对, 还需要将文件重置到头部, 如下:
#include
#include
#include
using namespace std;
int main()
{
ifstream in("test.txt");
string line;
// 下面代码有效
if(in)
{
while(getline(in, line))
{
cout << line.c_str() << endl;
}
}
in.clear();
in.seekg(0, ios::beg); // 指向begin处
// 会进入下面的if和while
if(in)
{
cout << "test" << endl;
while(getline(in, line)) // 与上面while功能一样
{
cout << line.c_str() << endl;
}
}
return 0;
}
总算是ok了。
最后看一个程序:
#include
#include
#include
using namespace std;
int main()
{
ifstream in("test.txt");
string line;
in.seekg(0, ios::end);
cout << in.tellg() << endl; // 文件大小
if(in.eof())
{
cout << "end of file" << endl;
}
else
{
cout << "in the file" << endl; // 进入到了这里
}
// in the file, 所以下面语句有效
in.seekg(0, ios::beg); // 指向begin处
// 下面代码有效
if(in)
{
while(getline(in, line))
{
cout << line.c_str() << endl;
}
}
return 0;
}
end of file的时候, seek是无效的, 必须先clear.