C++文件的读取写入入门小例子
来源:互联网 发布:襄阳淘宝生态城骗局 编辑:程序博客网 时间:2024/06/11 13:31
#include <fstream>#include <iostream>#include <iomanip>using namespace std;int main(){ int i; cout<<"选择功能:\n1:write\n2.read\n"; cin>>i; switch(i) { case 1: { char filename1[256]; cout<<"输入要写入的文件名"<<endl; cin>>filename1; int row,col; cout<<"输入矩阵的行,列"<<endl; cin>>row>>col; float *p=new float[col*row]; if(p==NULL) { cout<<"allocalte error"<<endl; return -1; } ofstream outfile(filename1); if(!outfile) { cout<<"can not open the outfile"<<endl; return -1; } outfile<<row<<" "<<col<<endl; for(int i=0;i<row;i++) { char buff[300]; for(int j=0;j<col;j++) { cin>>p[i*col+j]; //sprintf(buff,"%f",p[i*col+j]); outfile<<setiosflags(ios::fixed)<<setprecision(6)<<p[i*col+j]<<" "; } outfile<<endl; //outfile<<buff<<endl; } outfile.close(); delete []p; break; } case 2: { char filename2[256]; cout<<"输入要读入的文件名"<<endl; cin>>filename2; ifstream infile(filename2); if(!infile) { cout<<"cannot open file2"<<endl; return -1; } float ch[9]; int count,row,col; float sum; //infile.unsetf(ios::skipws); infile>>row>>col; count=row*col; for(int i=0;i<count;i++) { infile>>ch[i]; //count++; cout<<ch[i]<<" "; sum+=ch[i]; } cout<<endl; cout<<sum/count<<endl; infile.close(); break; } default: cout<<"wrong input"<<endl; break; } return 0;}
注意:infile(ifstream) outfile(ofstream)的使用和cin,cout类似,支持各种基本类型。
阅读全文
0 0
- C++文件的读取写入入门小例子
- xerces C/C++ 读取XML文件的一个小例子
- c#excel文件的读取与写入
- C#xml文件的读取写入
- 【C++】文件的读取与写入
- 【C#】关于文件的写入与读取
- 【C++】文件的读取与写入
- [C#]多线程读取数组的小例子
- android(安卓)sd卡写入 读取文件的例子
- 文件的读取写入
- C文件 写入 读取 函数
- C读取ini文件的例子
- 常见的C文件操作,读取,写入,获取文件指针
- C语言和C++的文件读取、写入函数
- C语言的文件读取与写入操作
- C语言中打开文件读取,写入的操作
- C语言/C++文件的读取与写入操作
- c语言 读取文件内容 文件写入
- java中Array/List/Map/Object与Json互相转换详解
- Android优化的十个要注意的问题
- struts2的struts.xml文件的固定格式
- windows下创建.gitignore文件(git提交时忽略某些文件)
- HTML5利用Canvas模拟上下扫描动画实现
- C++文件的读取写入入门小例子
- getComputedStyle
- AbstractQueuedSynchronizer
- TextView动态改变显示的内容
- 360搜索框上浮fixed的效果
- Android框架的收藏
- 输入一个一维数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
- bzoj 1662: [Usaco2006 Nov]Round Numbers 圆环数(枚举)
- dubbo+zookeeper+dubbo-admin环境的搭建