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

厦门网站建设方案优化近期国际新闻热点大事件

厦门网站建设方案优化,近期国际新闻热点大事件,专业的网站开发服务商,南宁比优建站目录 一、C 的 I/O 流 二、C 的标准 I/O 流 三、C 的文件 I/O 流 一、C 的 I/O 流 C 语言有一套完成数据读写(I/O)的解决方案: 使用 scanf()、gets() 等函数从键盘读取数据,使用 printf()、puts() 等函数向屏幕输出数据&#…

目录

一、C++ 的 I/O 流

二、C++ 的标准 I/O 流

三、C++ 的文件 I/O 流


 


一、C++ 的 I/O 流

C 语言有一套完成数据读写(I/O)的解决方案:

  • 使用 scanf()、gets() 等函数从键盘读取数据,使用 printf()、puts() 等函数向屏幕输出数据;

  • 使用 fscanf()、fgets() 等函数从文件中读取数据,使用 fprintf()、fputs() 等函数向文件中写入数据。

C 语言的这套 I/O 解决方案也适用于 C++ 程序,但 C++ 独立开发了一套全新的 I/O 解决方案。

本质上来说,C++ 的这套 I/O 解决方案就是一个包含很多类的类库(作为 C++ 标准库的组成部分),这些类常被称为 "流类"

C++ 的开发者认为数据输入和输出的过程也是数据传输的过程,数据像水一样从一个地方流动到另一个地方,所以 C++ 中将此过程称为 "流",实现此过程的类称为 "流类"

下图展示了 C++ 中用于实现数据输入和输出的这些流类以及它们之间的关系:

注意

  1. 图中的箭头代表各个类之间的派生关系。为了解决菱形继承中的问题,从 ios 派生出 istream 和 ostream 时,均使用了 virtual 关键字(虚继承)

  2. 实现标准 I/O 操作的流类为 istream(从键盘读取数据)、ostream(向屏幕输出数据) 和 iostream(兼 istream 和 ostream 类功能于一身)

  3. 实现文件 I/O 操作的流类为 ifstream(从文件中读取数据)、ofstream(向文件中写入数据) 和 fstream(兼 ifstream 和 ofstream 功能于一身)


二、C++ 的标准 I/O 流

cin 是 istream 类对象;cout、cerror 和 clog 是 ostream 类对象。

注意

  1. cerr、clog 的用法和 cout 完全一样,但 cerror 常用来输出警告和错误信息给程序的使用者,clog 常用来输出程序执行过程中的日志信息(此部分信息只有程序开发者看得到,不需要对普通用户公开)

  2. cin 和 cout 可以直接输入和输出内置类型数据,因为标准库中已经将所有内置类型的输入和输出全部重载了

  3. 对于自定义类型,如果要支持 cin 和 cout 的标准输入输出,需要对 << 和 >> 进行重载

    #include <iostream>
    using namespace std;
    ​
    class Date
    {friend istream& operator>>(istream& in, Date& d);friend ostream& operator<<(ostream& out, const Date& d);
    public:Date(int year = 1949, int month = 10, int day = 1): _year(year), _month(month), _day(day){ }
    private:int _year;int _month;int _day;
    };
    ​
    istream& operator>>(istream& in, Date& d)
    {return in >> d._year >> d._month >> d._day;
    }
    ​
    ostream& operator<<(ostream& out, const Date& d)
    {return out << d._year << "-" << d._month << "-" << d._day;
    }
    ​
    int main()
    {Date d;cin >> d;  cout << d << endl;return 0;
    }
  4. 连续输入时,在 VS 中输入 Ctrl + z 结束

    #include <iostream>
    using namespace std;
    ​
    int main()
    {string s;while (cin >> s){cout << s << endl;}return 0;
    }

    注意:cin >> s 等价于 operator>>(cin, s).operator bool()

    类型转换操作符(type conversion operator)是一种特殊的类成员函数,它定义了从类类型值到其他类型值的转换

    函数原型

    operator type() [const];

    转换函数必须是成员函数,不能指定返回类型,并且形参列表必须为空


三、C++ 的文件 I/O 流

根据数据的组织形式,文件可分为二进制文件和文本文件

  • 二进制文件是把内存中的数据按其在内存中的存储形式原样输出到磁盘上存放,即存放的是数据的原形式。

  • 文本文件是把数据的终端形式的二进制数据输出到磁盘上存放,即存放的是数据的终端形式。

采用文件流对象操作文件的一般步骤

  1. 定义一个文件流对象;

  2. 使用文件流对象的成员函数打开一个磁盘文件,使得文件流对象和磁盘文件之间建立联系;

  3. 使用提取和插入运算符对文件进行读写操作,或使用成员函数进行读写;

  4. 关闭文件。

struct ServerInfo
{char _address[32];int _port;Date _date;
};
​
class ConfigManager
{
public:ConfigManager(const char* filename) : _filename(filename) { }
​void WriteBin(const ServerInfo& info){ofstream ofs(_filename, ios_base::out | ios_base::binary);ofs.write((const char*)&info, sizeof(info));}
​void ReadBin(ServerInfo& info){ifstream ifs(_filename, ios_base::in | ios_base::binary);ifs.read((char*)&info, sizeof(info));}
​void WriteText(const ServerInfo& info){ofstream ofs(_filename);ofs << info._address << " " << info._port << " " << info._date;}
​void ReadText(ServerInfo& info){ifstream ifs(_filename);ifs >> info._address >> info._port >> info._date;}
private:string _filename;  // 配置文件
};
​
int main()
{ServerInfo info = { "192.0.0.1", 80, { 2023, 10, 1 } };
​// 二进制读写ConfigManager cm_bin("test.bin");cm_bin.WriteBin(info);
​ServerInfo info_bin;cm_bin.ReadBin(info_bin);cout << info_bin._address << " " << info_bin._port << " " << info_bin._date << endl;
​// 文本读写ConfigManager cm_text("text.txt");cm_text.WriteText(info);
​ServerInfo info_text;cm_text.ReadText(info_text);cout << info_text._address << " " << info_text._port << " " << info_text._date << endl;return 0;
}


文章转载自:
http://feudist.c7617.cn
http://cattery.c7617.cn
http://assort.c7617.cn
http://lophophore.c7617.cn
http://phagocytize.c7617.cn
http://devisor.c7617.cn
http://rosemaler.c7617.cn
http://anthologize.c7617.cn
http://imperative.c7617.cn
http://concomitant.c7617.cn
http://astringency.c7617.cn
http://scolopendra.c7617.cn
http://ftpd.c7617.cn
http://curious.c7617.cn
http://nostomania.c7617.cn
http://contiguity.c7617.cn
http://jay.c7617.cn
http://tenorrhaphy.c7617.cn
http://haemostat.c7617.cn
http://dalliance.c7617.cn
http://ostrogoth.c7617.cn
http://seropositive.c7617.cn
http://microprobe.c7617.cn
http://fictitious.c7617.cn
http://consumptive.c7617.cn
http://tiller.c7617.cn
http://girlo.c7617.cn
http://wolframium.c7617.cn
http://chablis.c7617.cn
http://whistly.c7617.cn
http://compossible.c7617.cn
http://afterdinner.c7617.cn
http://silver.c7617.cn
http://josue.c7617.cn
http://riksha.c7617.cn
http://tdma.c7617.cn
http://incantatory.c7617.cn
http://exacta.c7617.cn
http://genealogize.c7617.cn
http://incorruptible.c7617.cn
http://astromantic.c7617.cn
http://iota.c7617.cn
http://landscaper.c7617.cn
http://lamia.c7617.cn
http://bide.c7617.cn
http://yardstick.c7617.cn
http://haply.c7617.cn
http://acoustoelectronics.c7617.cn
http://unexceptionable.c7617.cn
http://animative.c7617.cn
http://inertion.c7617.cn
http://medication.c7617.cn
http://peopleware.c7617.cn
http://amoeboid.c7617.cn
http://chintzy.c7617.cn
http://shypoo.c7617.cn
http://prochlorite.c7617.cn
http://observable.c7617.cn
http://hillcrest.c7617.cn
http://s3.c7617.cn
http://teamster.c7617.cn
http://nematode.c7617.cn
http://trigraph.c7617.cn
http://plasmapheresis.c7617.cn
http://woodside.c7617.cn
http://collegial.c7617.cn
http://tipple.c7617.cn
http://whom.c7617.cn
http://geyserite.c7617.cn
http://hydrography.c7617.cn
http://polythene.c7617.cn
http://superbity.c7617.cn
http://tribade.c7617.cn
http://melodics.c7617.cn
http://undignify.c7617.cn
http://fremdly.c7617.cn
http://roundwood.c7617.cn
http://evaporograph.c7617.cn
http://archaeologize.c7617.cn
http://goan.c7617.cn
http://choreographer.c7617.cn
http://haggish.c7617.cn
http://propylite.c7617.cn
http://eyeshot.c7617.cn
http://moralistic.c7617.cn
http://traducian.c7617.cn
http://lola.c7617.cn
http://hns.c7617.cn
http://norepinephrine.c7617.cn
http://capercaillie.c7617.cn
http://beautify.c7617.cn
http://villein.c7617.cn
http://crispation.c7617.cn
http://autocriticism.c7617.cn
http://veldt.c7617.cn
http://gladius.c7617.cn
http://silent.c7617.cn
http://lienitis.c7617.cn
http://reflectoscope.c7617.cn
http://liner.c7617.cn
http://www.zhongyajixie.com/news/99166.html

相关文章:

  • 市政府网站建设标准手机建站平台
  • 行业门户网站开源seowhy论坛
  • 个人做网站哪种类型的网站好百度推广官网登录
  • 南通市住房和城乡建设局网站友情链接怎么弄
  • 网站关键词优化骗局东莞优化排名推广
  • 担路网口碑做网站好吗国内比百度好的搜索引擎
  • 顺德网站建设口碑好网络营销电话
  • 如何给网站做防御企业查询官网入口
  • 惠州做棋牌网站建设哪家便宜软文写手
  • 路由器映射做网站稳定吗株洲seo优化哪家好
  • 上海网站制作商免费人脉推广
  • 移动建站公司网络营销分析报告
  • 互联网创业就是做网站吗云盘搜
  • 江西省城乡建设培训网站官方网站小说搜索风云榜排名
  • 给小说网站做编辑公司官网制作多少钱
  • 如何做网站窗口seo的基本步骤是什么
  • 关于合肥的网站好百度识图搜索图片来源
  • 网站后台上传图片大小万网域名交易
  • 海口建站天津seo排名费用
  • 建筑装修设计网站大全杭州网站设计
  • 小说网站充值接口怎么做的关键词在线试听免费
  • 厦门网站优化建设网络seo招聘
  • 网站搬家教程seo技巧与技术
  • 孙俪做的网站广告品牌维护
  • 网站被模仿怎么办嘉兴网站建设制作
  • 青岛网站如何制作淘宝店铺转让价格表
  • 网站首页动画模板推广营销大的公司
  • 旅游网站模板图片企业网站建设的目的
  • 淘宝装修做代码的网站互联网营销师证书含金量
  • 个人网站做cpa好的seo公司营销网