1. fstream 继承自iostream --> 要包含头文件#include<fstream>

2. 建立文件流对象

3. 打开文件夹

4. 测试是否打开成功

5. 进行读写操作

6. 关闭文件

#include<iostream>
#include<fstream> using namespace std; int main(){
ifstream ifile;
ofstream ofile; ifile.open("d:\\fileIn.txt");
ofile.open("d:\\fileOut.txt"); if (ifile.fail() || ofile.fail()){
cerr << "open file fail\n";
return EXIT_FAILURE;
} char ch;
ch = ifile.get();
cout << ch << endl;
while (!ifile.eof()){
ofile.put(ch);
ch = ifile.get();
} ifile.close();
ofile.close(); int i;
cin >> i;
return ;
}

输入三个学生的姓名,学好,年龄和住址,并存入文件中,再从文件中读出来:

 #include<iostream>
#include<fstream>
using namespace std; class student{
public:
char name[];
int num;
int age;
char addr[];
friend ostream & operator<<(ostream &out, student &s);
friend istream & operator>>(istream &in, student &s);
};
ostream & operator<<(ostream &out, student &s){
out << s.name << " " << s.num << " " << s.age << " " << s.addr << endl;
return out;
}
istream & operator>>(istream &in, student &s){
in >> s.name >> s.num >> s.age >> s.addr;
return in;
}
int main(){
ifstream ifile;
ofstream ofile;
ofile.open("d:\\s.txt"); student s;
for (int i = ; i <= ; i++){
cout << "请输入第" << i << "个学生的姓名 学号 年龄 地址" << endl;
cin >> s; //调用>>运算符重载函数,输入学生信息
ofile << s; //调用<<运算符重载函数,将学生信息写入到文件中
}
ofile.close(); cout << "\n读出文件内容" << endl;
ifile.open("d:\\s.txt");
ifile >> s;
while (!ifile.eof()){
cout << s;
ifile >> s;
}
ifile.close();
int i;
cin >> i;
return ;
}

结果:

c++文件输入输出流fstream,对输入>>和输出<<重载的更多相关文章

  1. C++输入输出流 cin/cout 及格式化输出简介

    C++ 可通过流的概念进行程序与外界环境( 用户.文件等 )之间的交互.流是一种将数据自源( source )推送至目的地( destination )的管道.在 C++ 中,与标准输入/输出相关的流 ...

  2. C++ 输入输出流 文本文件 二进制文件读写

    文本文件/ASCII文件(能直接显示内容,费存储空间):文件中每一个字节中均以ASCII代码形式存放数据,即一个字节存放一个字符,这个文件就是ASCII文件或称字符文件. 二进制文件(不能显示内容,节 ...

  3. C++ STL——输入输出流

    [TOC] 注:原创不易,转载请务必注明原作者和出处,感谢支持! 注:内容来自某培训课程,不一定完全正确! 一 缓冲区 (1)标准输入:从键盘输入数据到程序(input) (2)标准输出:程序数据输出 ...

  4. Day 18:SequenceInputStream、合并切割mp3、对象输入输出流对象

    SequenceInputStream用例题讲述用法 需求:1.把a.txt与b.txt 文件的内容合并 2.把a.txt与b.txt .c.txt文件的内容合并 import java.io.Fil ...

  5. Java工具类-输入输出流

    输入输出流 1.概念 输入输出流:文件复制,上传 输出流: System.out.println() 写操作,程序将字符流写入到"目的地",比如打印机和文件等 输入流 :Scann ...

  6. iostream/fstream中的输入输出流指针的绑定,tie函数的使用。

      为了兼容c语言的输入输出,c++里面采用tie将输入输出流经行绑定,所以cin/cout并不是独立的.当执行cin时,cout同时会被执行.反之亦然. by defalut,cin is tied ...

  7. C++标准程序库的输入输出流(I/O Stream)复制文件(4种方法)

    使用C++标准程序库的输入输出流(I/O   Stream)复制文件,存在许多的方法, 方法一:逐个字符复制#include   <   fstream   > std::ifstream ...

  8. Java文件操作与输入输出流

    文件操作 package ch15; import java.io.*; /** * Created by Jiqing on 2016/12/28. */ public class FileTest ...

  9. 2013级C++第15周(春)项目——输入输出流及文件文件操作

    课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 本周程序阅读及程序调试中须要的文件,请到htt ...

随机推荐

  1. Maven build lifecycle

    Clean Lifecycle 运行mvn clean执行clean生命周期,包含三个生命周期阶段: pre-clean clean post-clean clean:clean会删除一次构建后的输出 ...

  2. iOS开发常用代码块

    遍历可变数组的同时删除数组元素 NSMutableArray *copyArray = [NSMutableArray arrayWithArray:array]; NSString *str1 = ...

  3. 当我们在谈论kmeans(2)

        本稿为初稿,后续可能还会修改:如果转载,请务必保留源地址,非常感谢! 博客园:http://www.cnblogs.com/data-miner/ 其他:建设中- 当我们在谈论kmeans(2 ...

  4. Ubuntu16.04 LTS下apt安装WireShark

    Ubuntu16.04 LTS下apt安装WireShark 安装与配置 首先通过apt安装WireShark: $ sudo apt install wireshark 会同时安装许多的依赖包,其中 ...

  5. 揭开C++类中虚表的“神秘面纱”

    C++类中的虚表结构是C++对象模型中一个重要的知识点,这里咱们就来深入分析下虚表的在内存中的结构. C++一个类中有虚函数的话就会有一个虚表指针,其指向对应的虚表,一般一个类只会有一个虚表,每个虚表 ...

  6. Error invoking SqlProvider method (com.github.abel533.mapper.MapperProvider.dynamicSQL). Cause: java.lang.InstantiationException: com.github.abel533.mapper.MapperProvider

    org.apache.ibatis.exceptions.PersistenceException: ### Error querying database.  Cause: org.apache.i ...

  7. 【repost】JavaScript 事件模型 事件处理机制

    什么是事件? 事件(Event)是JavaScript应用跳动的心脏 ,也是把所有东西粘在一起的胶水.当我们与浏览器中 Web 页面进行某些类型的交互时,事件就发生了.事件可能是用户在某些内容上的点击 ...

  8. Ext动态加载Toolbar

    在使用Ext的GridPanel时候,有时候需要面板不用重新加载而去更新Store或者Toolbar,Store的方法有很多,例如官方api给我们提供的Store.load(),Store.reLoa ...

  9. IFC是什么

    IFC是用EXPRESS语言来描述的一种数据格式 IFC的物理文件 为了数据交换的目的,STEP标准Prat 21规定了正文文件的结构,认为一个STEP文件或一个Part 21文件包括两端:头段和数据 ...

  10. bzoj 2434 阿狸的打字机 fail树的性质

    如果a串是另b串的后缀,那么在trie图上沿着b的fail指针走一定可以走到a串. 而a串在b串里出现多少次就是它是多少个前缀的后缀. 所以把fail边反向建树维护个dfs序就行了. 并不是很难... ...