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. Z-Stack协议中几个重要概念的理解

    1. 原语     ZigBee设备在工作时,各种不同的任务在不同的层次上执行,通过层的服务,完成所要执行的任务.每一层的服务主要完成两种功能:根据它的下层服务要求,为上层提供相应的服务:另一咱是根据 ...

  2. css 优先级

    css优先级的四大原则: 原则一: 继承不如指定 如果某样式是继承来的永远不如具体指定的优先级高.例子1:CODE:<style type="text/css"> &l ...

  3. (十)boost库之多线程

    (十)boost库之多线程 1.创建线程 使用boost库可以方便的创建一个线程,并提供最多支持9个参数的线程函数,相对于void*来说,方便了很多,创建线程主要提供了一下3种方式: 线程库头文件:# ...

  4. POSIX和SYSTEM的消息队列应该注意的问题

    首先看看POSIX的代码: 1.posix_mq_server.c #include <mqueue.h>#include <sys/stat.h>#include <s ...

  5. codevs1039 数的划分

    题目描述 Description 将整数n分成k份,且每份不能为空,任意两种划分方案不能相同(不考虑顺序). 例如:n=7,k=3,下面三种划分方案被认为是相同的. 1 1 5 1 5 1 5 1 1 ...

  6. ASP.NET-Web-API-Poster.pdf flow chart

    下载地址

  7. C#操作XML的完整例子——XmlDocument篇(转载,仅做学习之用)

    原文地址:http://www.cnblogs.com/serenatao/archive/2012/09/05/2672621.html 这是一个用c#控制台程序下,  用XmlDocument 进 ...

  8. C编程技巧

    1,attempted assighnment to literal if (i == 3) { //codes } else if (4 == 4); 2,引用数组元素相当于对指针加上偏移量的引用 ...

  9. 全国计算机等级考试二级教程-C语言程序设计_第2章_C程序设计的初步知识

    正负号与被除数一致. 3 % (-5) == 3 (-3) % 5 == -3 不用求余运算符,求出余数. int x, y; 答:x - x / y * y; const int i = 10; c ...

  10. open函数

    open函数用来打开文件 其语法为:open(name[, mode[, buffering]]) open函数使用一个文件名作为唯一的强制参数,然后返回一个文件对象.模式(mode)和缓冲(buff ...