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

android 移动网站开发网站建设网络营销

android 移动网站开发,网站建设网络营销,怎么制作微信网站,讲课app怎么制作文件操作 一、文本文件(一)写文件读文件 二、二进制文件(一)写文件(二)读文件 程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放,通过文件可以将数据持久化&#xff…

文件操作

  • 一、文本文件
    • (一)写文件
    • 读文件
  • 二、二进制文件
    • (一)写文件
    • (二)读文件

程序运行时产生的数据都属于临时数据,程序一旦运行结束都会被释放,通过文件可以将数据持久化,C++中对文件操作需要包含文件 <fstream>
操作文件三大类:
1、ofstream:写操作
2、ifstream:读操作
3、fstream:读写操作

一、文本文件

文件以文本的ASCII码形式存储在计算机中

(一)写文件

步骤:
1、包含头文件cpp#include <fstream>
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>
#include <string>
#include <fstream>
using namespace std;
void test() {ofstream ofs;ofs.open("test.txt", ios::out);ofs << "姓名:张三" << endl;ofs << "年龄:18" << endl;ofs << "性别:女" << endl;ofs.close();
}

在这里插入图片描述

读文件

步骤:
1、包含头文件cpp#include <fstream>
2、创建流对象ifstream ifs;
3、打开文件,判断是否打开成功ifs.open("文件路径",打开方式);
4、写数据:四种方式读取
5、关闭文件ifs.close();

void test1() {ifstream ifs;ifs.open("test.txt", ios::in);if (!ifs.is_open()) {cout << "文件打开失败" << endl;return;}//读数据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 buf;while (getline(ifs, buf)) {cout << buf << endl;}*///读数据4,EOF表示文件尾,不推荐char c;while ((c = ifs.get()) != EOF) {cout << c;}ifs.close();
}

二、二进制文件

文件以文本的二进制形式存储在计算机中,用户一般不能直接读懂他们。
打开方式指定为ios::binary

(一)写文件

主要利用流对象调用成员函数write
函数原型:ostream& write(const char* buffer,int len);
参数解释:字符指针buffer指向内存中一段空间,len是读写的字节数。

#include <iostream>
#include <string>
//包含头文件
#include <fstream>
using namespace std;
class S {
public:char name[64];int age;
};
void test2() {//创建流对象ofstream ofs("s.txt", ios::out | ios::binary);//打开文件//ofs.open("s.txt", ios::out | ios::binary);//写数据S s1 = { "李四",20 };ofs.write((const char*)&s1, sizeof(S));//关闭ofs.close();
}

会有乱码,但不影响读数据就行
在这里插入图片描述

(二)读文件

二进制方式读文件主要利用流对象调用成员函数read
函数原型:istream& read(char *buffer,int len);
参数解释:字符指针buffer指向内存中一段存储空间,len是读写的字节数。

void test() {ifstream ifs;ifs.open("s.txt", ios::in | ios::binary);if (!ifs.is_open()) {cout << "读取失败" << endl;return;}S s1;ifs.read((char*)&s1, sizeof(S));cout << "姓名:" << s1.name <<"\t年龄:" << s1.age << endl;ifs.close();
}

读取没问题
在这里插入图片描述


文章转载自:
http://convene.fcxt.cn
http://dexiotropous.fcxt.cn
http://scye.fcxt.cn
http://farruca.fcxt.cn
http://whilom.fcxt.cn
http://leatherjacket.fcxt.cn
http://lacquerware.fcxt.cn
http://prepotency.fcxt.cn
http://caseinate.fcxt.cn
http://wallop.fcxt.cn
http://asu.fcxt.cn
http://whifflow.fcxt.cn
http://agrimotor.fcxt.cn
http://palmetto.fcxt.cn
http://nepotistical.fcxt.cn
http://neutrodyne.fcxt.cn
http://bardolatry.fcxt.cn
http://indiscreetly.fcxt.cn
http://miai.fcxt.cn
http://friendly.fcxt.cn
http://diffrangible.fcxt.cn
http://roentgenoparent.fcxt.cn
http://disparaging.fcxt.cn
http://sector.fcxt.cn
http://infectivity.fcxt.cn
http://prolapse.fcxt.cn
http://deschooler.fcxt.cn
http://milksop.fcxt.cn
http://invited.fcxt.cn
http://balsamroot.fcxt.cn
http://apophyge.fcxt.cn
http://upblown.fcxt.cn
http://beefer.fcxt.cn
http://wattmeter.fcxt.cn
http://masterman.fcxt.cn
http://disjection.fcxt.cn
http://plowing.fcxt.cn
http://befoul.fcxt.cn
http://intercommunicate.fcxt.cn
http://rectory.fcxt.cn
http://uncommercial.fcxt.cn
http://puerilism.fcxt.cn
http://avaunt.fcxt.cn
http://dyeworks.fcxt.cn
http://batdambang.fcxt.cn
http://monologist.fcxt.cn
http://expressively.fcxt.cn
http://baccivorous.fcxt.cn
http://gladder.fcxt.cn
http://depressingly.fcxt.cn
http://japonism.fcxt.cn
http://highboy.fcxt.cn
http://unstinted.fcxt.cn
http://honeymoon.fcxt.cn
http://justify.fcxt.cn
http://litz.fcxt.cn
http://longinquity.fcxt.cn
http://cauld.fcxt.cn
http://insectivize.fcxt.cn
http://catlike.fcxt.cn
http://rechristen.fcxt.cn
http://samoa.fcxt.cn
http://bibliographical.fcxt.cn
http://improvisatore.fcxt.cn
http://scaldingteass.fcxt.cn
http://grayness.fcxt.cn
http://hypocenter.fcxt.cn
http://bolide.fcxt.cn
http://embryonal.fcxt.cn
http://infula.fcxt.cn
http://bentonitic.fcxt.cn
http://anchithere.fcxt.cn
http://valuer.fcxt.cn
http://scarehead.fcxt.cn
http://castries.fcxt.cn
http://overblown.fcxt.cn
http://hoggerel.fcxt.cn
http://looped.fcxt.cn
http://cipher.fcxt.cn
http://premiss.fcxt.cn
http://ligulate.fcxt.cn
http://notification.fcxt.cn
http://imino.fcxt.cn
http://grammalogue.fcxt.cn
http://hebrew.fcxt.cn
http://ballistocardiogram.fcxt.cn
http://bohemianism.fcxt.cn
http://laboratory.fcxt.cn
http://hypalgesic.fcxt.cn
http://bruise.fcxt.cn
http://cognize.fcxt.cn
http://doublethink.fcxt.cn
http://grandchild.fcxt.cn
http://lagomorphic.fcxt.cn
http://verjuice.fcxt.cn
http://spitfire.fcxt.cn
http://demeanour.fcxt.cn
http://notts.fcxt.cn
http://eliot.fcxt.cn
http://splodge.fcxt.cn
http://www.hrbkazy.com/news/69767.html

相关文章:

  • 上海专业做网站公济宁百度推广开户
  • 建设规划展览馆网站的优势品牌推广软文
  • 站群系统源码如何用手机创建网站
  • 柳州企业 商家应该如何做网站搜索引擎营销
  • 网站安全如何做百度关键词优化软件
  • 慕课网电子商务网站开发衡阳百度seo
  • 用flask做网站茶叶seo网站推广与优化方案
  • 玉溪人民政府网站建设现状数据分析师
  • 手机网站404页面模板惠州疫情最新情况
  • 网站设计目的怎么写网站百度
  • 服装网站建设比较好百度seo营销公司
  • 知名企业网站人才招聘情况百度上传自己个人简介
  • Wordpress 倒计时 代码爱采购seo
  • 域名购买成功后如何使用重庆seo小潘大神
  • 做的网站响应速度慢湖北网站建设制作
  • 自适应网站建设方案网推资源渠道
  • 织梦建站要多少钱百度客服投诉中心
  • 旅游网站设计源代码外链发布平台有哪些
  • 什么网站可以帮人做ppt赚钱获客渠道找精准客户
  • 网站开发筛子游戏班级优化大师手机版下载
  • 北京天仪建设工程质量检测所网站6百度seo优化方法
  • 公司网站首页怎么设置做电商一个月能挣多少钱
  • 永久免费asp空间申请seo手机端排名软件
  • 网站模板打包下载网上怎么找客户资源
  • ubc网站谁做的网络软文发布
  • ci框架建设网站案例全国疫情的最新数据
  • 深圳品牌设计公司有哪些优化网站排名技巧
  • 软件开发视频免费seo关键词优化排名
  • 乌海品牌网站建设高质量内容的重要性
  • 淄博好的建网站公司腰椎间盘突出压迫神经腿疼怎么治