一、文本文件
1、写文件
2、读文件
读文件的四种方式:
#include<iostream>
using namespace std;
#include<fstream>
#include<string>
void test(){
ifstream ifs;
ifs.open("test.txt",ios::in);
if (!ifs.is_open()){cout << "打开文件失败!"<<endl;}
char buf[1024] = {0};
//第一种
// while(ifs >> buf){
// cout << buf << endl;
// }
//第二种
// while(ifs.getline(buf,sizeof(buf))){
// cout << buf << endl;
// }
//第三种
// string buf1;
// while(getline(ifs,buf1)){
// cout << buf1 << endl;
// }
//第四种
char c;
while((c = ifs.get()) != EOF){
cout << c;
}
ifs.close();
}
int main()
{
test();
return 0;
}
二、二进制文件