#include <fstream>
1 fstream
2 ifstream
3 ofstream
4 seekg
5 seekp
6 tellg
7 tellp
1 fstream
打开输入输出文件流
#include <iostream>
#include <fstream> void main()
{
std::fstream fio("F:\\1.txt", std::ios::in | std::ios::out); fio << "hello" << std::endl;//写入
fio << "world" << std::endl;
fio << "hello" << std::endl;
fio << "china" << std::endl; fio.seekg(std::ios::beg);//文件指针回到文件开头,beg,begin for (int i = ; i < ; i++)//读取
{
char str[] = { };
fio.getline(str, );
std::cout << str << std::endl;
} fio.close();//关闭文件 system("pause");
}
2 ifstream
打开输入文件流
#include <iostream>
#include <fstream> void main()
{
std::ifstream fin("F:\\1.txt");//创建读取文件流 char str[] = { }; fin >> str;//读取 fin.close();//关闭文件 std::cout << str; system("pause");
}
3 ofstream
打开输出文件流
打开文件,按行写入
std::endl换行
#include <iostream>
#include <fstream> void main()
{
std::ofstream fout; fout.open("F:\\1.txt");//打开文件,如果没有文件,将会创建新的文件 fout << "hello" << std::endl;//写入,std::endl换行
fout << "china" << std::endl;
fout << "hello" << std::endl;
fout << "world" << std::endl; fout.close();//关闭文件 system("pause");
}
std::ios::app
打开文件以便在文件的尾部添加数据
#include <iostream>
#include <fstream> void main()
{
std::ofstream fout("F:\\1.txt", std::ios::app);//打开输出文件流 fout << "hello world hello china";//写入 fout.close();//关闭文件 system("pause");
}
复制文本
ifstream负责读取,ofstream负责写入
按照字节的方式读写二进制
文件加密解密都需要字节的方式
#include <iostream>
#include <fstream> //按照字节的方式读写二进制
//文件加密解密都需要字节的方式 void main()
{
std::ifstream fin("F:\\1.txt", std::ios::binary);//需要复制的文件
std::ofstream fout("F:\\new.txt", std::ios::binary);//保存复制后的文件 if (!fin || !fout)
{
std::cout << "文件打开失败" << std::endl;
return;
} std::cout << "文件复制开始" << std::endl;
char ch = ; while (fout && fin.get(ch))//引用的方式读取到一个字符
{
fout.put(ch);//写入一个字节
} fin.close();//关闭文件
fout.close();//关闭文件 std::cout << "文件复制完成" << std::endl; system("pause");
}
4 seekg
对输入流操作:seekg()
fin.seekg(9, std::ios::beg);//从开始往后9个字符
fin.seekg(-9, std::ios::end);//从尾部倒数9个字符
#include <iostream>
#include <fstream> void main()//二进制随机位置读取
{
double db[] = { 1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.1 }; std::ofstream fout("F:\\1.txt", std::ios::binary);//写入文件
fout.write((char *)db, );//写入以字节为单位,因此转换为char类型
fout.close(); double *p = new double[]; std::ifstream fin("F:\\1.txt", std::ios::binary);//读取文件 fin.seekg(-, std::ios::end);//指针到达尾部前40个字节 fin.read((char *)p, );
fin.close(); for (int i = ; i < ; i++)
{
std::cout << p[i] << std::endl;
} system("pause");
}
5 seekp
对输出流操作:seekp()
seekp(9, std::ios::beg);//确定随机位置进行读写
#include <iostream>
#include <fstream> void main()
{
std::ofstream fout("F:\\1.txt");//写入文件
fout << "1234567890abcdefghijklmn";
fout.close();//关闭文件 std::ofstream Fout("F:\\1.txt", std::ios::in | std::ios::out);//写入文件
char str[] = "helloworld"; Fout.seekp(, std::ios::beg);//确定随机位置进行读写 Fout << str;//输出到文件
Fout.close();//关闭文件 std::ifstream fin("F:\\1.txt");//读取文件
char ch; while (fin.get(ch))//打印
{
std::cout << ch;
}
fin.close();//关闭文件 system("pause");
}
6 tellg
7 tellp
对输出流操作:tellp()
long size = Fout.tellp();//当前位置距离begin有多少个字节,到尾部可以获取文件大小
#include <iostream>
#include <fstream> void main()
{
std::ofstream fout("F:\\1.txt");//写入文件
fout << "1234567890abcdefghijklmn";
fout.close();//关闭文件 std::ofstream Fout("F:\\1.txt", std::ios::in | std::ios::out);//写入文件
char str[] = "helloworld"; Fout.seekp(, std::ios::end);//确定随机位置进行读写
long size = Fout.tellp();//当前位置距离begin有多少个字节,到尾部可以获取文件大小 Fout << str;//输出到文件
Fout.close();//关闭文件 std::ifstream fin("F:\\1.txt");//读取文件
char ch; while (fin.get(ch))//打印
{
std::cout << ch;
}
fin.close();//关闭文件 system("pause");
}
#include <fstream>的更多相关文章
- 浅谈JSP中include指令与include动作标识的区别
JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...
- Entity Framework 6 Recipes 2nd Edition(13-9)译 -> 避免Include
问题 你想不用Include()方法,立即加载一下相关的集合,并想通过EF的CodeFirst方式实现. 解决方案 假设你有一个如Figure 13-14所示的模型: Figure 13-14. A ...
- error RC1015: cannot open include file 'afxres.h' 解决办法
在为WindowsPhone8程序添加本地化的过程中遇到这个问题: 问题原因就是afxres.h文件缺失,下载它,放到VS安装目录下的VS\include目录下就可以了(选择目录的时候注意对应对版本) ...
- Mybatis常用总结:参数,返回,执行sql,include等
1.参数注入1.1用#{0},#{1}的形式,0代表第一个参数,1代表第二个参数 public List<RecordVo> queryList(String workerId, Inte ...
- jsp中的@include与jsp:include区别详解
1 前言 搞java开发的人也许都知道在jsp中引入项目中其他文件有如下两种方式 <%@include file="xxx.jsp"%> <jsp:include ...
- JSP中编译指令include与动作指令include的区别
include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改, 否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如 ...
- C/C++ 中的include
当需要使用已有的方法或库时, 可以将它们的头文件#include进来. #include会在preprocess过程中被替换成它包含的代码. 头文件中包含了需要使用的函数/变量的声明. 当然声明与定义 ...
- 织梦多语言站点,{dede:include filename=''/}引入问题
织梦模板include插入非模板目录文件出现"无法在这个位置找到"错误的解决办法 以下是dede V55_UTF8 查dede include标签手册 (3) include 引入 ...
- PHP 站点相对包含,路径的问题解决方法(include,require)
以前看了,很多框架,基本上很少使用相对路径包含.而一般很多做php web站点,喜欢用相对路径. 认为这样,无论目录放到那里. 只要跟另外目录关系一致.那么就不会出现问题.如果一个站点,一般都认为,如 ...
- 如何让include标签包裹的布局置于屏幕最下方?
如何让一个Layout 始终在屏幕的下方 我想让<include layout="@layout/bottom" />一直在屏幕下,怎么做? 1.相对布局中用属性 a ...
随机推荐
- SDK Manager 报错:Connection timed out: connect
安装Eclipse的安卓开发环境的时候,安装sdk时报错,出现: 解决办法: 1.选择左上角的Tools 2.选择Options,勾选下面红色框的东西 3. 4.重新重启一下sdk manager即可
- logcat错误日志
http://www.crifan.com/android_log_to_file/ http://www.iteye.com/problems/85431 http://www.cnblogs.co ...
- fragment低版本
http://bbs.csdn.net/topics/390271980 Fragment框架开发东西确实很方便,但是恼人的是从4.0才开始支持.以前的版本必须用兼容模式开发,本人在网上找了大量资料, ...
- WSImport
http://www.cnblogs.com/simle/archive/2011/11/03/2233417.html
- MEMS陀螺仪(gyroscope)的结构
MEMS陀螺仪(gyroscope)的设计和工作原理可能各种各样,但是公开的MEMS陀螺仪均采用振动物体传感角速度的概念.利用振动来诱导和探测科里奥利力而设计的MEMS陀螺仪没有旋转部件.不需要轴承, ...
- UML_组件图
简介 众所周知,组件图是用来描述系统中的各组件之间的关系.首先我们必须知道组件的定义是什么,然后组件之间有哪些关系.理清楚这些,我们在以后的设计中才能 派上用场.UML语言对组件的定义已发生了巨大变化 ...
- NSTimer你真的会用了吗
http://www.cnblogs.com/smileEvday/archive/2012/12/21/NSTimer.html
- iOS GCD详解
前言 对初学者来说,GCD似乎是一道迈不过去的坎,很多人在同步.异步.串行.并行和死锁这几个名词的漩涡中渐渐放弃治疗.本文将使用图文表并茂的方式给大家形象地解释其中的原理和规律. 线程.任务和队列的概 ...
- 格而知之1:UIButton中imageView和titleLabel的位置调整
在使用UIButton时,有时候需要调整按钮内部的imageView和titleLabel的位置和尺寸.在默认情况下,按钮内部的imageView和titleLabel的显示效果是图片在左文字在右,然 ...
- Sumsets(3sum问题,枚举d,c二分a+b)
Sumsets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9997 Accepted: 2736 Descripti ...