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

2. 建立文件流对象

3. 打开文件夹

4. 测试是否打开成功

5. 进行读写操作

6. 关闭文件

  1. #include<iostream>
  2. #include<fstream>
  3.  
  4. using namespace std;
  5.  
  6. int main(){
  7. ifstream ifile;
  8. ofstream ofile;
  9.  
  10. ifile.open("d:\\fileIn.txt");
  11. ofile.open("d:\\fileOut.txt");
  12.  
  13. if (ifile.fail() || ofile.fail()){
  14. cerr << "open file fail\n";
  15. return EXIT_FAILURE;
  16. }
  17.  
  18. char ch;
  19. ch = ifile.get();
  20. cout << ch << endl;
  21. while (!ifile.eof()){
  22. ofile.put(ch);
  23. ch = ifile.get();
  24. }
  25.  
  26. ifile.close();
  27. ofile.close();
  28.  
  29. int i;
  30. cin >> i;
  31. return ;
  32. }

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

  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4.  
  5. class student{
  6. public:
  7. char name[];
  8. int num;
  9. int age;
  10. char addr[];
  11. friend ostream & operator<<(ostream &out, student &s);
  12. friend istream & operator>>(istream &in, student &s);
  13. };
  14. ostream & operator<<(ostream &out, student &s){
  15. out << s.name << " " << s.num << " " << s.age << " " << s.addr << endl;
  16. return out;
  17. }
  18. istream & operator>>(istream &in, student &s){
  19. in >> s.name >> s.num >> s.age >> s.addr;
  20. return in;
  21. }
  22. int main(){
  23. ifstream ifile;
  24. ofstream ofile;
  25. ofile.open("d:\\s.txt");
  26.  
  27. student s;
  28. for (int i = ; i <= ; i++){
  29. cout << "请输入第" << i << "个学生的姓名 学号 年龄 地址" << endl;
  30. cin >> s; //调用>>运算符重载函数,输入学生信息
  31. ofile << s; //调用<<运算符重载函数,将学生信息写入到文件中
  32. }
  33. ofile.close();
  34.  
  35. cout << "\n读出文件内容" << endl;
  36. ifile.open("d:\\s.txt");
  37. ifile >> s;
  38. while (!ifile.eof()){
  39. cout << s;
  40. ifile >> s;
  41. }
  42. ifile.close();
  43. int i;
  44. cin >> i;
  45. return ;
  46. }

结果:

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. 跳跃的舞者,舞蹈链(Dancing Links)算法——求解精确覆盖问题

    精确覆盖问题的定义:给定一个由0-1组成的矩阵,是否能找到一个行的集合,使得集合中每一列都恰好包含一个1 例如:如下的矩阵 就包含了这样一个集合(第1.4.5行) 如何利用给定的矩阵求出相应的行的集合 ...

  2. hdu1695 GCD(莫比乌斯反演)

    题意:求(1,b)区间和(1,d)区间里面gcd(x, y) = k的数的对数(1<=x<=b , 1<= y <= d). 知识点: 莫比乌斯反演/*12*/ 线性筛求莫比乌 ...

  3. webgl动画小测试

    // MultiPoint.js (c) 2012 matsuda // Vertex shader program var VSHADER_SOURCE = 'attribute vec4 a_Po ...

  4. [LeetCode] Delete Duplicate Emails 删除重复邮箱

    Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...

  5. FineUI(专业版)v3.0.0 发布,手机、平板和桌面全支持!

    FineUI(专业版)v3.0.0 已经正式发布,全面支持手机.平板和桌面!       自 2008 年 4 月发布第一个版本,我们持续更新了 126 个版本,拥有 16000 多位注册用户,130 ...

  6. sstream

    sstream用法 #include<iostream> #include<sstream> #include<string> using namespace st ...

  7. BPM问题

    1.安装XFormDesigner后编辑界面报错 解决方法:

  8. root与普通用户的切换

    普通用户切换到root用户:sudo su - root用户切换到普通用户:su henie

  9. vcf格式

    Variant Call Format(VCF)是一个用于存储基因序列突变信息的文本格式.表示单碱基突变, 插入/缺失, 拷贝数变异和结构变异等.BCF格式文件是VCF格式的二进制文件. CHROM ...

  10. IntelliJ运行下载的Servlet时报错 Error running Tomcat 8.5.8: Unable to open debugger port (127.0.0.1:49551): java.net.SocketException

    学习Java Servlet时,从Wrox上下载了示例代码,准备run/debug时发现以下错误: Error running Tomcat 8.5.8: Unable to open debugge ...