1.利用string中的getline函数,直接按整行读取。
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ifstream readFile;
readFile.open("D:/Cppcode/INS_practice/dataset/GNSS-RTK.txt", ios::in);
if (readFile.is_open())
{
cout << "Sucess open!" << endl;
string str;
while (getline(readFile,str))
{
cout << str << endl;
}
}
else
{
cout << "Open Failure!" << endl;
}
readFile.close();
return 0;
}
文件读取完毕,返回。
2.利用readFile中的getline方法
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ifstream readFile;
readFile.open("D:/Cppcode/INS_practice/dataset/GNSS-RTK.txt", ios::in);
if (readFile.is_open())
{
cout << "Open sucess!" << endl;
char buff[1000] = { 0 };
while (readFile.getline(buff, sizeof(buff)))
{
cout << buff << endl;
}
}
else
{
cout << "Open failure!" << endl;
}
readFile.close();
return 0;
}
3.利readFile中的get函数
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ifstream readFile;
readFile.open("D:/Cppcode/INS_practice/dataset/GNSS-RTK.txt", ios::in);
if (readFile.is_open())
{
cout << "Open sucess!" << endl;
char buff = 0;
while ((buff = readFile.get()) != EOF)
{
cout << buff;
}
}
else
{
cout << "Open fail!" << endl;
}
readFile.close();
return 0;
}
第一第二种方法读取速度比第三种要快