一、文本文件
ifstream:(input)、读文件
ofstream:(output)、写操作
fstream:读写文件
(1)、写文件
----------------------------------------
#include<fstream>
ofstream ofs; //创建流对象
ofs.open("文件路径", 打开方式); //打开文件
ofs<<"写入的数据"; //ofs,往文件上输出,cout 往屏幕上输出
ofs.close(); //关闭文件
----------------------------------------
文件打开方式:ios::in(读文件)、 ios::out(写文件)、
ios::ate(初始位置:文件尾巴)、
ios::app(以追加方式写文件)、append
ios::trunc(如果文件存在,先删除再创建)、
ios::binary(二进制方式)
二进制方式写文件:ios::binary | ios::in
#include<iostream>
using namespace std;
#include<fstream>
void test01()
{
ofstream ofs;
ofs.open("test.txt", ios::out); //打开文件所在路径,该文件目录下自动创建保存
ofs << "姓名:张三" << endl;
ofs << "性别:男" << endl;
ofs.close();
}
int main()
{
test01();
system("pause");
return 0;
}
(2)、读文件
1、头文件
#include<fstream>
2、创建流对象
ifstream ifs;
3、打开文件,并判断是否打开成功
ifs.open("test.txt",ios::in);
// ifs.open("D:/ttt/a.text", ios::in);
if ( ! (ifs.is_open() )
{
cout<<"文件打开失败"<<endl;
}
4、读文件(四种方法)
string sbuf;
while( getline( ifs, sbuf) )
{
cout<<sbuf<<endl;
}
5、关闭文件
ifs.close();
-----------------------------------
3、读文件的四种方式
推荐使用:(3) (2) (4),不推荐使用(1)
(1)、输出格式不正常(不推荐使用,它是按空格分开,进行读取),读的可能出来形式不满要求
char buf[1024] = { 0 };
while( ifs>>buf )
{
cout<<buf<<endl; //一连串一连串读,一连串读完换行
} //一连串指的是:没有空格的一连串
(2)、推荐使用(输出正常)
char buf[1024] = { 0 };
while( ifs.getline(buf,sizeof(buf)) )
{
cout<<buf<<endl; //文件里的内容一行一行读,一行读完换行。
}
(3)推荐使用(输出正常)
string sbuf;
while( getline( ifs, sbuf ) )
{
cout<<sbuf<<endl; //文件里的内容一行一行读,一行读完换行。
}
(4) 、输出正常
char c;
while( (c = ifs.get()) != EOF)
{
cout<<c;
}
#include<iostream>
using namespace std;
#include<fstream>
#include<string>
void test01() //推荐使用第三种方式,其次是2,4,最好不用1
{
//创建流对象
ifstream ifs;
//打开文件,并判断是否打开
ifs.open("D:/ttt/c.text", ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败" << endl;
return;
}
//读数据
//第一种
//cout << "第一种方式读文件" << endl;
//char buf1[1024] = { 0 };
//cout << endl << "-01-------" << endl;
//while (ifs >> buf1)
//{
// cout << buf1 << endl; //一连串一连串读,一连串读完(没有空格)换行
//}
//cout << endl << "-03-------" << endl;
//第二种
//cout << "第二种方式读文件" << endl;
//char buf2[1024] = { 0 };
//while (ifs.getline(buf2, sizeof(buf2)))
//{
// cout << buf2 << endl; //一行读完换行,一行一行读
//}
//第三种
//cout << "第三种方式读文件" << endl;
//string sbuf;
//while (getline(ifs, sbuf)) //getline(ifs,sbuf) 全局函数
//{
// cout << sbuf << endl; //一行读完换行,一行一行读
//}
//第四种
cout << "第四种方式读文件" << endl;
char c;
while ((c = ifs.get()) != EOF) //EOF : end of file
{
cout << c;
}
//关闭文件
ifs.close();
}
int main()
{
test01();
system("pause");
return 0;
}
二、二进制文件
文件路径:
D:/ttt/a.text (TEXT文件)
D:/ttt/d.txt (文本文档)
1、二进制写文件
//二进制写文件 - 可以写自定义数据类型
———————————
class Person
{
public:
char m_Name[64]; //最好不要使用string,读写文件会出现问题
int m_Age;
};
-----------------------------------
#include<fstream>
ofstream ofs;
//ofstream ofs("D:/ttt/d.txt", ios::binary | ios::out ); //建流对象,同时打开文件
ofs.open("D:/ttt/d.txt",ios::binary | ios::out);
Person p = { "张三",18 };
ofs.write( (const char*)&p, sizeof(Person) );
ofs.close();
——————————
#include<iostream>
using namespace std;
#include<fstream>
//二进制写文件 - 可以写自定义数据类型
//最好不要使用string,会出现问题
class Person
{
public:
char m_Name[64];
int m_Age;
};
void test01()
{
ofstream ofs;
//创建流对象,同时打开文件
//ofstream ofs("D:/ttt/d.txt", ios::binary | ios::out);
ofs.open("D:/ttt/d.txt",ios::binary | ios::out);
Person p = { "张三",19 };
ofs.write((const char*)&p, sizeof(Person));
ofs.close();
}
int main()
{
test01();
system("pause");
return 0;
}
2、二进制读文件
#include<iostream>
using namespace std;
#include<fstream>
class Person
{
public:
char m_Name[64];
int m_Age;
};
void test01()
{
ifstream ifs("D:/ttt/d.txt", ios::binary | ios::in);
//ifs.open
if ( !(ifs.is_open()) )
{
cout<<"文件打开失败"<<endl;
return;
}
Person p;
ifs.read((char*)&p, sizeof(Person));
cout << p.m_Name << " " << p.m_Age << endl;
ifs.close();
}
int main()
{
test01();
system("pause");
return 0;
}