c++文件输入输出流fstream,对输入>>和输出<<重载
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,对输入>>和输出<<重载的更多相关文章
- C++输入输出流 cin/cout 及格式化输出简介
C++ 可通过流的概念进行程序与外界环境( 用户.文件等 )之间的交互.流是一种将数据自源( source )推送至目的地( destination )的管道.在 C++ 中,与标准输入/输出相关的流 ...
- C++ 输入输出流 文本文件 二进制文件读写
文本文件/ASCII文件(能直接显示内容,费存储空间):文件中每一个字节中均以ASCII代码形式存放数据,即一个字节存放一个字符,这个文件就是ASCII文件或称字符文件. 二进制文件(不能显示内容,节 ...
- C++ STL——输入输出流
[TOC] 注:原创不易,转载请务必注明原作者和出处,感谢支持! 注:内容来自某培训课程,不一定完全正确! 一 缓冲区 (1)标准输入:从键盘输入数据到程序(input) (2)标准输出:程序数据输出 ...
- Day 18:SequenceInputStream、合并切割mp3、对象输入输出流对象
SequenceInputStream用例题讲述用法 需求:1.把a.txt与b.txt 文件的内容合并 2.把a.txt与b.txt .c.txt文件的内容合并 import java.io.Fil ...
- Java工具类-输入输出流
输入输出流 1.概念 输入输出流:文件复制,上传 输出流: System.out.println() 写操作,程序将字符流写入到"目的地",比如打印机和文件等 输入流 :Scann ...
- iostream/fstream中的输入输出流指针的绑定,tie函数的使用。
为了兼容c语言的输入输出,c++里面采用tie将输入输出流经行绑定,所以cin/cout并不是独立的.当执行cin时,cout同时会被执行.反之亦然. by defalut,cin is tied ...
- C++标准程序库的输入输出流(I/O Stream)复制文件(4种方法)
使用C++标准程序库的输入输出流(I/O Stream)复制文件,存在许多的方法, 方法一:逐个字符复制#include < fstream > std::ifstream ...
- Java文件操作与输入输出流
文件操作 package ch15; import java.io.*; /** * Created by Jiqing on 2016/12/28. */ public class FileTest ...
- 2013级C++第15周(春)项目——输入输出流及文件文件操作
课程首页在:http://blog.csdn.net/sxhelijian/article/details/11890759.内有完整教学方案及资源链接 本周程序阅读及程序调试中须要的文件,请到htt ...
随机推荐
- Fragment之间传值
Activity: String myArguments; public String getarguments() { return myArguments; } public void ...
- js获取页面url中的各项值
一. 通过window.location获取各项参数 1.获取页面完整的url url = window.location.href; 2.获取页面的域名 host = window.location ...
- freeswitch 使用mysql替换默认的sqlite
转自 80000hz.com freeswitch 使用mysql替换默认的sqlite No Reply , Posted in 默认分类 on January 14, 2014 目标使用mysql ...
- ngx_http_fastcgi_module模块.md
ngx_http_fastcgi_module ngx_http_fastcgi_module模块允许将请求传递到FastCGI服务器. fastcgi_bind Syntax: fastcgi_bi ...
- $.ajax()方法详解
jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(p ...
- 篇二:JSON解析
前台以json形式的数据格式传到后台,代码如下: import net.sf.json.JSONObject; public class JsonUtility { /** * 解析JSON 对象为对 ...
- 【hrbust2293】棋盘村
题意 哈理工2016级新生程序设计全国邀请赛A题 http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&probl ...
- py-faster-rcnn搭配pycharm使用
先在ubuntu下配置好cuda.cudnn以及py-faster-rcnn,然后安装pycharm. 打开pycharm看py-faster-rcnn代码,import处各种红色下划曲线,提示报错. ...
- 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1878 Solved: 846[Submit][Status ...
- Django自定义模板
定义simple_tag步骤 一.创建templatetags文件 首先在app下创建templatetags文件:名字不许叫这个,不能改变. 二.在文件中创建一个py文件 文件名自定义 三.在创建的 ...