赞
踩
程序运行过程中产生的数据都属于临时数据,程序一旦运行结束就会被删除掉。
可以通过文件存储的方式将数据存储下来。
文件类型分类:
C++的头文件的包含:#include<fstream>
(文件流)
文件操作的三大类:
#include<fstream>
ofstream ofs;
ofs.open("文件路径",打开方式);
(需要带一个判断语句判断头文件的是否打开成功)ofs<<" ";
(or读数据)ofs.close();
//必须做操作打开方式:
代码 | 说明 |
---|---|
ios::in | 为读文件而打开文件 |
ios::out | 为写文件而打开文件 |
ios::ate | 初始位置 到 文件尾部 |
ios::app | 追加方式写文件(打开的时候直接跑到最后追加写) |
ios::trunc | 如果文件存在先删除,在创建新的 |
ios::binary | 二进制方式 |
注意:
ios::binary | ios::out
,即或操作endl
也可以 ofstream ofs;
//若文件存在先删除在创建一个新的,后以写的方式打开,之前没有创建使用open也能创建
ofs.open("test.txt",ios::out | ios::trunc);
ofs << "写入文件数据" << endl;
ofs << "张三,12" << endl;
//创建读文件流 ifstream ifs; //打开文件 ifs.open("test.txt", ios::in); //判断文件是否打开 if (!ifs.is_open()) { cout << "打开失败"<< endl; return; } //读数据(4种方式) char buf[1024] = {0}; while (ifs >> buf)//当读到文件尾时,会返回false { cout << buf << endl; } //关闭文件 ifs.close();
由于读取方式一般有4种,这里采用类的方式全部给出:
#include<iostream> #include<fstream> #include<string> using namespace std; class folder { public: folder(){} ~folder(){} void test01() { ofstream ofs; ofs.open("test.txt", ios::out | ios::trunc);//若文件存在先删除在创建一个新的,后以写的方式打开,之前没有创建使用open也能创建 ofs << "写入文件数据" << endl; ofs << "张三,12" << endl; } void test02() { //创建读文件流 //ifstream ifs; //打开文件 IFS.open("test.txt", ios::in); //判断文件是否打开 if (!IFS.is_open()) { cout << "打开失败"<< endl; return; } //读数据(4种方式) read_way_4(IFS); //关闭文件 IFS.close(); } void read_way_1(ifstream &ifs) //方式1 { char buf[1024] = {0}; while (ifs >> buf) { cout << buf << endl; } ifs.close(); } void read_way_2(ifstream &ifs) //方式2 { char buf[1024] = { 0 }; while (ifs.getline(buf,sizeof(buf))) { cout << buf << endl; } ifs.close(); } void read_way_3(ifstream &ifs)//方式3,推荐,长度可扩展 { string buf; while (getline(ifs, buf)) { cout << buf << endl; } ifs.close(); } void read_way_4(ifstream &ifs)//方式4,不推荐,费时间 { char c; while ((c = ifs.get())!=EOF)//EOF end of file (文件尾的意思) { cout << c; } ifs.close(); } private: ifstream IFS; }; int main() { folder f; f.test01();//写入数据 f.test02();//读数据 system("pause"); return 0; }
ios::binary
方式打开char[]
数组类型写入,不然会出现一定的问题二进制方式文件注意利用流对象调用成员函数 **write(x,x) **
即:函数原型:ostream& write(const char * buffer, int len);
解释:字符指针 buffer 指向内存中一段存储空间,len 是读写的字节数
class folder { public: void test03(person &p) { ofstream ofs("test_2.txt", ios::out | ios::binary);//以二进制方式打开写入 ofs.write((const char *)&p, sizeof(p));//强制转换成const类型 ofs.close(); } } class person { public: char P_Name[64]; int age; }; int main() { folder f; person p; p = { "李四",10 }; f.test03(p);//二进制写入自定义的person类型数据 system("pause"); return 0; }
函数原型:ostream& read(char * _Str, int len);
class person { public: char P_Name[64]; int age; }; class folder { public: void test04()//读取二进制数据操作 { ifstream ifs("test_2.txt",ios::in | ios::binary); //最好加上一个是否打开的判断 if (!ifs.is_open()) { cout << "打开失败" << endl; return; } person px; ifs.read((char*)&px,sizeof(person));//将读到数据存到px中 ifs.close(); cout << "姓名:" << px.P_Name << " 年龄:" << px.age << endl; } } int main() { folder f; person p; p = { "李四",10 }; f.test03(p);//写数据 f.test04();//读取数据 system("pause"); return 0; }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。