#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 ...
随机推荐
- java面试题大全-基础方面
Java基础方面: 1.作用域public,private,protected,以及不写时的区别答:区别如下:作用域 当前类 同一package 子孙类 ...
- SQL表名,应该用复数还是单数
用单数形式更佳,理由如下: 1.概念直观. 你有一个袋子,里面有好多个苹果,你会说这是个苹果袋.但无论里面有0,1,百万个苹果,它依然是个袋子.表也是如此,表明需要描述清楚,表里面包含的对象,而非有多 ...
- C语言的本质(16)——函数接口的传入参数与传出参数
如果函数接口有指针参数,既可以把指针所指向的数据传给函数使用(称为传入参数),也可以由函数填充指针所指的内存空间,传回给调用者使用(称为传出参数),例如strcpy的函数原型为 char *strcp ...
- C语言---整型字符串转换
C语言提供了几个标准库函数,能够将随意类型(整型.长整型.浮点型等)的数字转换为字符串.下面是用itoa()函数将整数转 换为字符串的一个样例: # include <stdio.h> ...
- ORA-03113 通信通道的文件结尾(ORA-19804 ORA-16038-归档空间满的处理方法)
1.数据库启动报错SQL> startupORACLE 例程已经启动. Total System Global Area 1887350784 bytesFixed Size 2176848 b ...
- 【概率论】【POJ 3682】【King Arthur's Birthday Celebration】
题意:进行翻硬币实验,若k次向上则结束,进行第n次实验需花费2*n-1的费用,询问期望结束次数及期望结束费用 设F[i]为第i次结束时的概率 F[i]= c(i-1,k-1)*p^k*(1-p)^( ...
- 浅谈web前端就业的学习路线
初级前端 主要学习三个部分:HTML,CSS,JavaScript 一.html + css部分: 这部分特别简单,到网上搜资料,书籍视频非常多.css中盒子模型,流动,block,inline,层叠 ...
- 蜗牛爱课 - iOS7、8模态半透明弹出框
//源Controller中跳转方法实现 MKDialogController *controller = [[MKDialogController alloc] init]; controller. ...
- [Linked List]Remove Nth Node From End of List
Total Accepted: 84303 Total Submissions: 302714 Difficulty: Easy Given a linked list, remove the nth ...
- mysql学习(九)sql语句
SQL种类: DDL:数据定义语言 DML:数据操作语言 DQL:数据查询语言 DCL:数据控制语言 DDL: show databases; //查询数据库 create database if n ...