示例:
#include<iostream>
using namespace std;
#include<fstream>
#include<string>
void test01()
{
//创建文件流
ifstream ifs;
//打开文件 并判断文件是否打开成功
ifs.open("test.txt", ios::in);
if (!ifs.is_open())
{
cout << "打开文件失败"<<endl;
return;
}
//读取文件
//读取方式1
//char buf[1024] = { 0 };
//while (ifs >> buf)
//{
// cout << buf << endl;
//}
//读取方式二
//char buf[1024] = { 0 };
//while (ifs.getline(buf, sizeof(buf)))
//{
// cout << buf << endl;
//}
//读取方式三
//string buf;
//while (getline(ifs, buf))
//{
// cout << buf << endl;;
//}
//读取方式四
//char c;
//while ((c = ifs.get()) != EOF)
//{
// cout << c;
//}
//关闭文件
ifs.close();
}
int main()
{
test01();
system("pause");
return 0;
}