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>的更多相关文章

  1. 浅谈JSP中include指令与include动作标识的区别

    JSP中主要包含三大指令,分别是page,include,taglib.本篇主要提及include指令. include指令使用格式:<%@ include file="文件的绝对路径 ...

  2. Entity Framework 6 Recipes 2nd Edition(13-9)译 -> 避免Include

    问题 你想不用Include()方法,立即加载一下相关的集合,并想通过EF的CodeFirst方式实现. 解决方案 假设你有一个如Figure 13-14所示的模型: Figure 13-14. A ...

  3. error RC1015: cannot open include file 'afxres.h' 解决办法

    在为WindowsPhone8程序添加本地化的过程中遇到这个问题: 问题原因就是afxres.h文件缺失,下载它,放到VS安装目录下的VS\include目录下就可以了(选择目录的时候注意对应对版本) ...

  4. Mybatis常用总结:参数,返回,执行sql,include等

    1.参数注入1.1用#{0},#{1}的形式,0代表第一个参数,1代表第二个参数 public List<RecordVo> queryList(String workerId, Inte ...

  5. jsp中的@include与jsp:include区别详解

    1 前言 搞java开发的人也许都知道在jsp中引入项目中其他文件有如下两种方式 <%@include file="xxx.jsp"%> <jsp:include ...

  6. JSP中编译指令include与动作指令include的区别

    include指令是编译阶段的指令,即include所包含的文件的内容是编译的时候插入到JSP文件中,JSP引擎在判断JSP页面未被修改, 否则视为已被修改.由于被包含的文件是在编译时才插入的,因此如 ...

  7. C/C++ 中的include

    当需要使用已有的方法或库时, 可以将它们的头文件#include进来. #include会在preprocess过程中被替换成它包含的代码. 头文件中包含了需要使用的函数/变量的声明. 当然声明与定义 ...

  8. 织梦多语言站点,{dede:include filename=''/}引入问题

    织梦模板include插入非模板目录文件出现"无法在这个位置找到"错误的解决办法 以下是dede V55_UTF8 查dede include标签手册 (3) include 引入 ...

  9. PHP 站点相对包含,路径的问题解决方法(include,require)

    以前看了,很多框架,基本上很少使用相对路径包含.而一般很多做php web站点,喜欢用相对路径. 认为这样,无论目录放到那里. 只要跟另外目录关系一致.那么就不会出现问题.如果一个站点,一般都认为,如 ...

  10. 如何让include标签包裹的布局置于屏幕最下方?

    如何让一个Layout 始终在屏幕的下方 我想让<include layout="@layout/bottom" />一直在屏幕下,怎么做? 1.相对布局中用属性  a ...

随机推荐

  1. SQL Server 823,824 错误

    第一: 823错误只代表.SQL server 要向操作系统申请一个页面读写的时候遇到Windos读取或写入失败.823错误是读写请求时发生的 和读写的内容没有关系.823与SQL server 本身 ...

  2. 如何让旧版IE浏览器认识HTML5元素

    <!--[if lt IE 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js" ...

  3. 进程退出前删除自身EXE

    进程退出前删除自身EXE 下面的代码由Gary Nebbett写就.Gary Nebbett乃是WINDOWS NT/2000 NATIVE API REFERENCE的作者.乃NT系统一等一的高手. ...

  4. perl /m 当作多行处理

    高级用法 多行匹配: zjtest7-frontend:/root/0825# cat a2.pl print "1111111111111\n"; my $_="abc ...

  5. cf467A George and Accommodation

    A. George and Accommodation time limit per test 1 second memory limit per test 256 megabytes input s ...

  6. hdu 2665 Kth number_划分树

    题意:求区间[a,b]的第k大 因为多次询问要用到划分树 #include <iostream> #include<cstdio> #include<algorithm& ...

  7. 安装oracle客户端(navicat for oracle)

    本文使用Navicat for Oracle工具连接oracle 安装的过程我就不在此赘述,跟一般软件的安装过程类似.下面主要讲解Navicat的配置. 1.启动该工具,出现如下的开始界面,单击“连接 ...

  8. 很好很实用的.net、网站系统后台模板

    本模板是程序园给大家提供的应用系统开发后台模板,主要使用div+css布局实现,菜单使用了ddaccordion.js菜单控件. 转载请标明:http://www.kwstu.com/ArticleV ...

  9. 3.数据库操作相关术语,Oracle认证,insert into,批量插入,update tablename set,delete和truncate的差别,sql文件导入

     1相关术语 语句 含义 操作 DML语句 (Data Manipulation Language) 数据库操作语言 insert update delete select DDL语言 (Date ...

  10. 【并查集+拓扑排序】【HDU1811】【Rank of Tetris】

    题意:给你3种关系 A=B,A>B,A<B 问是否排名方式唯一,或者存在矛盾 解 1.读入数据先处理 =号 用并查集的祖先作为代表元素,其他儿子节点都等于跟这个点重叠. 再读入 '< ...