#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 ...
随机推荐
- Nginx 变量漫谈(一)
Nginx 的配置文件使用的就是一门微型的编程语言,许多真实世界里的 Nginx 配置文件其实就是一个一个的小程序.当然,是不是“图灵完全的”暂且不论,至少据我观察,它在设计上受 Perl 和 Bou ...
- system.exit(0) vs system.exit(1)
2.解析 查看java.lang.System的源代码,我们可以找到System.exit(status)这个方法的说明,代码如下: /** * Terminates the currently ru ...
- asp.net基础学习笔记
原文地址:http://blog.csdn.net/oxoxzhu/article/details/8652530 1.概论 浏览器-服务器 B/S 浏览的 浏览器和服务器之间的交互,形成上 ...
- UML_交互图
交互图(Interaction Diagram)用来描述系统中的对象是如何进行相互作用的.即一组对象是如何进行消息传递的. 当交互图建模时,通常既包括对象(每个对象都扮演某一特定的角色),又包括消息( ...
- 【LeetCode练习题】Validate Binary Search Tree
Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST) ...
- nyoj 927 The partial sum problem(dfs)
描述 One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choo ...
- 深信服笔试题(网络project师售后)
总共同拥有3到大题, 1选择 主要有ip地址计算.http协议.vrrp协议. 2.主要是linux填空题 a.linux显示全部系统载入模块____ b.写出linux的两个开机启动程序___.__ ...
- Android得到一个闹钟在第三方
收集报警信息 闹铃时间,闹铃备注信息 闹铃引起系统变化的点: 1. Send Notification (正点闹钟能够设置不发送) 2. Play audio 闹铃信息结构体 ClockInfo{ S ...
- CSS3 布局
1.1 列布局 CSS3中新出现的多列布局(multi-column)是传统HTML网页中块状布局模式的有力扩充.这种新语法能够让WEB开发人员轻松的让文本呈现多列显示.我们知道,当一行文字太长时 ...
- 解决UITableView数据没有充满屏幕时,显示多余的空白cell的问题
#pragma mark 去除多余的线 -(void) clearExtrLine{ UIView *view = [[UIView alloc] init]; view.backgroundColo ...