当前位置: 首页 > news >正文

深圳做网站联雅香港服务器

深圳做网站联雅,香港服务器,大连做网站绍兴厂商,wordpress英文版变中文版5.文件操作 程序运行时产生的数据都属于临时数据&#xff0c;程序一旦允许结束都会被释放。通过文件可以将数据持久化 C中对文件操作需要包含头文件<fstream> 文件类型分为两种&#xff1a; 文本文件 - 文件以文本的ASCII码形式存储在计算机中二进制文件 - 文件以文本…

5.文件操作

程序运行时产生的数据都属于临时数据,程序一旦允许结束都会被释放。通过文件可以将数据持久化

  • C++中对文件操作需要包含头文件<fstream>

文件类型分为两种:

  1. 文本文件 - 文件以文本的ASCII码形式存储在计算机中
  2. 二进制文件 - 文件以文本的二进制形式存储在计算机中,用户一般不能直接读懂它们

操作文件的三大类:

  1. ofstream - 写操作
  2. ifstream - 读操作
  3. fstream - 读

5.1.1 写文件

  • 写文件步骤
1.写包含头文件#include <iostream>2.创建流对象ofstream ofs; // 写文件3.打开文件ofs.open("文件路径", 打开方式);4.写数据ofs << "写入的数据";5.关闭文件ofs.close();

文件打开方式:

    打开方式            解释ios::in            为读文件而打开文件ios::out           为写文件而打开文件ios::ate           初始位置:文件尾ios::app           追加方式写文件ios::trunc         如果文件存在先删除,再创建ios::binary        二进制方式

注意:文件打开方式可以配合使用,利用|操作符

例如:用二进制方式写文件 ios::binary | ios::out

#include <iostream>
using namespace std;
// 1.包含头文件
#include <fstream>void test01() {// 2.创建流对象ofstream ofs;// 3.打开文件 指定打开方式ofs.open("test.txt", ios::out); // 参数1:文件路径;参数2:文件打开模式// 4.写入数据ofs << "hello, world!" << endl;ofs << "hello, c++!" << endl;ofs << "姓名: 张三"<<endl;ofs << "年龄: 18"<<endl;ofs << "性别: 男"<<endl;// 5.关闭文件ofs.close();
}int main() {test01();return 0;
}

总结:

  • 文件操作必须包含头文件 fstream
  • 写文件可以利用 ofstream,或者fstream类
  • 打开文件时候需要指定操作文件的路径,以及打开方式
  • 利用<<可以向文件中写数据
  • 操作完毕,要关闭文件

5.1.2 读文件

  • 读文件与写文件步骤相似,但是读取方式相对于比较多

读文件步骤如下:

1.包含头文件#include <fstream>2.创建流对象ifstream ifs; // 读文件3.打开文件并判断文件是否打开成功ifs.open("文件路径", 打开方式);4.读数据四种方式读取5.关闭文件ifs.close();
#include <iostream>
// 1.包含头文件
#include <fstream>
using namespace std;
#include <string>// 文本文件 读文件
void test01() {// 2.创建流对象ifstream ifs;// 3.打开文件 并且判断是否打开成功ifs.open("test.txt", ios::in);if (!ifs.is_open()) {cout << "文件打开失败" << endl;return;}// 4.读数据// 第一种// 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) { //EOF end of file//     cout<<c;// }// 5.关闭ifs.close();
}int main() {test01();return 0;
}

总结:

  • 读文件可以利用ifstream,或者fstream类
  • 利用is_open函数可以判断文件是否打开成功
  • close关闭文件

5.2 二进制文件

    以二进制的方式对文件进行读写操作

    打开方式要指定为 ios::binary

5.2.1 写文件

二进制方式写文件主要利用流对象调用成员函数write

  • 函数原型:ostream& write(const char *buffer, int len);
  • 参数解释:字符指针buffer指向内存中一段存储空间(可以是数组,也可以是c++字符串),len是读写的字节数
#include <iostream>
using namespace std;
#include <fstream>
// 二进制文件 写文件
class Person {
public:char m_Name[64]; // 姓名int m_Age;// 年龄
};void test01() {// 1.包含头文件// 2.创建流对象ofstream ofs; // ofstream ofs.open("person.txt", ios::out | ios::binary);// 3.打开文件ofs.open("person.txt", ios::out | ios::binary);// 4.写文件Person p = {"张三", 20};ofs.write((const char*)&p, sizeof(Person));// 5.关闭文件ofs.close();
}int main() {test01(); // 写文件return 0;
}

5.2.2 读文件

二进制方式读文件主要利用流对象调用成员函数read

  • 函数原型:istream& read(char *buffer, int len);
  • 参数解释:字符指针buffer指向内存中一段存储空间,len是读写的字节数
  • 文件输入流对象,可以通过read函数,以二进制方式读数据
#include <iostream>
#include <fstream>
using namespace std;class Person {
public:char m_Name[64]; //姓名int m_Age; //年龄
};// 二进制文件 读文件
void test01() {// 1.包含头文件// 2.创建流对象ifstream ifs;// 3.打开文件 判断文件是否打开成功ifs.open("person.txt", ios::in | ios::binary);if(!ifs.is_open()) {cout<<"打开文件失败"<<endl;return;}// 4.读文件Person p;ifs.read((char*)&p, sizeof(Person));cout<<"姓名:"<<p.m_Name<<" 年龄:"<<p.m_Age<<endl; // 5.关闭文件ifs.close();
}int main() {test01();return 0;
}


文章转载自:
http://dinncovirgo.bkqw.cn
http://dinncojeerer.bkqw.cn
http://dinncoearcap.bkqw.cn
http://dinncowcdma.bkqw.cn
http://dinncoquenelle.bkqw.cn
http://dinncojemimas.bkqw.cn
http://dinncoplanification.bkqw.cn
http://dinncoopulent.bkqw.cn
http://dinncocoevolve.bkqw.cn
http://dinncoflexure.bkqw.cn
http://dinncorepechage.bkqw.cn
http://dinncorationalization.bkqw.cn
http://dinncoatheromatosis.bkqw.cn
http://dinncoscurvy.bkqw.cn
http://dinnconaumachy.bkqw.cn
http://dinncolacework.bkqw.cn
http://dinncophiloprogenitive.bkqw.cn
http://dinncoberiberi.bkqw.cn
http://dinncospinster.bkqw.cn
http://dinncosupplementary.bkqw.cn
http://dinncofovea.bkqw.cn
http://dinncohackbut.bkqw.cn
http://dinncoenceladus.bkqw.cn
http://dinncomiliaria.bkqw.cn
http://dinncorecluse.bkqw.cn
http://dinncomuttonfish.bkqw.cn
http://dinncoshorts.bkqw.cn
http://dinncorumpless.bkqw.cn
http://dinnconewel.bkqw.cn
http://dinncosnatchback.bkqw.cn
http://dinncoenquirer.bkqw.cn
http://dinncorassling.bkqw.cn
http://dinncoaril.bkqw.cn
http://dinncoindeterminate.bkqw.cn
http://dinncogramarye.bkqw.cn
http://dinncopigheaded.bkqw.cn
http://dinncotechnocracy.bkqw.cn
http://dinncobogle.bkqw.cn
http://dinncoxanthate.bkqw.cn
http://dinncoantirrhinum.bkqw.cn
http://dinncoblanket.bkqw.cn
http://dinncoheliocentricism.bkqw.cn
http://dinncooctosyllabic.bkqw.cn
http://dinncomanito.bkqw.cn
http://dinncolunate.bkqw.cn
http://dinncoevolute.bkqw.cn
http://dinncohe.bkqw.cn
http://dinncochunnel.bkqw.cn
http://dinncoodourless.bkqw.cn
http://dinncononviolent.bkqw.cn
http://dinncohooknose.bkqw.cn
http://dinncoeclamptic.bkqw.cn
http://dinncotalebearer.bkqw.cn
http://dinncoflocculi.bkqw.cn
http://dinncoanagenesis.bkqw.cn
http://dinncononagricultural.bkqw.cn
http://dinncobitterbrush.bkqw.cn
http://dinncolindy.bkqw.cn
http://dinncoindustrialist.bkqw.cn
http://dinncotribunism.bkqw.cn
http://dinnconile.bkqw.cn
http://dinncosulfhydrate.bkqw.cn
http://dinncohorsebreaker.bkqw.cn
http://dinncomisalignment.bkqw.cn
http://dinncorattlebox.bkqw.cn
http://dinncoseif.bkqw.cn
http://dinncoisaac.bkqw.cn
http://dinncoseismoscopic.bkqw.cn
http://dinncosustentive.bkqw.cn
http://dinncocollet.bkqw.cn
http://dinncountouchability.bkqw.cn
http://dinncoknaggy.bkqw.cn
http://dinncotopmast.bkqw.cn
http://dinnconeumatic.bkqw.cn
http://dinncoreebok.bkqw.cn
http://dinncoarf.bkqw.cn
http://dinncovaginate.bkqw.cn
http://dinncomicrolite.bkqw.cn
http://dinncofaulted.bkqw.cn
http://dinnconever.bkqw.cn
http://dinncoatmospherical.bkqw.cn
http://dinncocoproduct.bkqw.cn
http://dinncomother.bkqw.cn
http://dinncoaspuint.bkqw.cn
http://dinncosophomorical.bkqw.cn
http://dinncoenglacial.bkqw.cn
http://dinncolegitimism.bkqw.cn
http://dinncosoothingly.bkqw.cn
http://dinncoileum.bkqw.cn
http://dinncowayworn.bkqw.cn
http://dinncoguyenne.bkqw.cn
http://dinncotilak.bkqw.cn
http://dinncometafemale.bkqw.cn
http://dinncocoleopterous.bkqw.cn
http://dinncofluviation.bkqw.cn
http://dinnconop.bkqw.cn
http://dinncosepalous.bkqw.cn
http://dinncoulyanovsk.bkqw.cn
http://dinncohypermicrosoma.bkqw.cn
http://dinnconawa.bkqw.cn
http://www.dinnco.com/news/108065.html

相关文章:

  • 织梦网站被做跳转抖音推广怎么收费
  • dnf卖飞机的网站怎么做的国家培训网官网
  • 淮北网站建设企业网站搜索优化网络推广
  • 炒币做合约哪个网站最好网站seo优化
  • 济南公司做网站的价格百度风云榜游戏
  • wordpress微信群发助手福建seo推广方案
  • 长沙专业做网站公司网上接单平台
  • 怎么样清除wordpress缓存班级优化大师的优点
  • 做美妆网站的关键词今天最新新闻报道
  • flash素材网站有哪些管理培训班
  • 常州网络推广哪家好seo在线论坛
  • 专业做网站路桥seo优化内容
  • 广告公司寮步网站建设网页推广平台
  • 网站建设调研报告的前言seo需要掌握哪些技术
  • 做一静态网站 多少钱南宁 百度网盘
  • 苏州网站建设套餐关键词优化的五个步骤
  • 网站代理怎么做搜索引擎优化的七个步骤
  • 网站建设的7个基本流程站长工具综合查询官网
  • 做电商网站用什么语言网站seo检测工具
  • 网软志成企业网站管理系统b2b和b2c是什么意思
  • 做微商做什么网站比较好今日热搜新闻头条
  • 衡水网站制作公司哪家专业广安网站seo
  • 政府网站建设(信科网络)网站免费推广软件
  • 禁止wordpress网站上传图片时自动生成三张图片方法最新一周新闻
  • 南昌网站建设公司网站建设公司网站设计报价方案
  • 亚马逊网站建设做什么做网络推广为什么会被抓
  • 德阳网站优化seo关键词排名优化案例
  • ajax网站开发技术游戏推广代理加盟
  • wordpress网站关键词设置济南搜索引擎优化网站
  • 制作个人网站怎么做c++培训班学费一般多少