字符串文件流的实现
来源:互联网 发布:linux端口映射 命令 编辑:程序博客网 时间:2024/06/10 10:46
#include <iostream>#include <fstream>using namespace std;int main(){ /*读取文件中的150个数字,算出和并写进去 */ //先把数字写进去 //构造函数法(建立流和文件的关系有两种方法) ifstream ifile1("/Users/riverspace/myCpp/Data1.txt",ios::in);//原始数据1 ofstream ofile1("/Users/riverspace/myCpp/Data1.txt",ios::out); ifstream ifile2("/Users/riverspace/myCpp/Data2.txt",ios::in);//原始数据2 ofstream ofile2("/Users/riverspace/myCpp/Data2.txt",ios::out); ofstream ofile3("/Users/riverspace/myCpp/Data3.txt",ios::out);//相加结果 ofstream ofile5("/Users/riverspace/myCpp/Data5.txt",ios::out);//相除结果 for(int i=0;i<150;i++){ ofile1<< i+1 << ' '; if ((i+1)%10==0){ //后面再++的这里要i+1 ofile1<<endl; } } for (int j=0;j<150;j++){ ofile2<< j+1<< ' '; if((j+1)%10==0){ ofile2<<endl; } } //求和与相除,结果写进去 int a[150],b[150],c[100],d[100]; for(int temp1=0;temp1<150;temp1++) { ifile1 >> a[temp1]; //用ifile1文件流对应的数字填满数组a ifile2 >> b[temp1]; c[temp1] = a[temp1] + b[temp1]; d[temp1] = a[temp1] / b[temp1]; //数组a和数组a的前n个元素相除,存在p1中然后出去 ofile3 << c[temp1] << ' '; ofile5 << d[temp1] << ' '; if ((temp1 + 1) % 10 == 0) { ofile3 << endl; ofile5 <<endl; } } //使用结束用,要切断流和文件的关系(只有一种方法) ifile1.close();ofile1.close();ifile2.close();ofile2.close();ofile3.close();ofile5.close(); /* 文件流的另一种方式*/ fstream file; //声明一个对象 file.open("/Users/riverspace/myCpp/Data6.txt");//用对象成员函数打开文件 if(!file){ //测试是否真的打开了文件 cout<<"can't open data6.txt"<<endl; exit(1); } //对文本文件进行处理 char ch1; char ch2[] = "Nanjing"; char ch3[20]; char *p4; cin.get(ch1); cout<<ch1<<endl; file<<ch2<<endl;cout<<"ch2='Nanjing'已输出到Data6.txt"<<endl; cout<<ch2<<endl; cout<<"请输入ch3,20位"<<endl; cin.getline(ch3,20,'l'); file<<ch3<<endl;cout<<"ch3已输出到Data6.txt"<<endl; file.close(); //关闭文本文件 p4=ch3; cout<<"p4="<<p4<<endl; return 0;}
阅读全文
0 0
- 字符串文件流的实现
- 一个使用shell脚本实现的修改文件中的字符串
- [Unix] shell脚本实现的修改文件中的字符串
- JS解析XML文件和字符串的跨浏览器实现
- base64实现文件与字符串之间的转换(转)
- 使用wxString实现字符串在一个文件里面的替换
- python实现查找文件名包含指定字符串的文件
- Java之Base64实现文件和字符串之间的转换
- 【头文件】c++实现字符串
- 在文件中替换字符串为指定字符串的实现函数
- Delphi 字符串/内存流/文件的转换
- 文件字符串的替换
- 用C#实现基于查寻字符串的文件行查询器(3)-设计与实现
- 字符串操作的实现
- 字符串的实现机制
- 实现字符串的反转
- 字符串的包含实现
- 实现字符串的连接
- Python:聚合函数(groupby)
- 23-IO流(演示键盘录入) 24-IO流(读取键盘录入) 25-IO流(转换流)123 26-IO流(转换流-需求演示)
- 如何解决python3中numpy报错No module named 'numpy'
- 修改linux中的网卡名
- 数组笔记
- 字符串文件流的实现
- (一)Node.js写入文件中文乱码问题
- Multisim的探针(Probe)及数字电路的一些调试技巧的应用
- 学习笔记6
- Servlet 文件上传
- centos7.2重新安装yum
- PTA 6-1 顺序表基本操作(10 分)
- 高德地图小实现
- poj2528&&线段树模板