C++将数据写入磁盘文件
2017-08-05 13:49
204 查看
/* 对磁盘文件作如下操作: (1) 在磁盘上建立一个文本文件,该文件中存放若干个实数; (2) 在磁盘上已有文本文件中,读出若干个实数,并求出其中的最大数、最小数和平均值; (3) 向文件追加记录、浏览文本文件、将一个文件复制到另一个文件中(定义类来完成)。 */ #include<fstream> #include<iostream> //在测试时发现不加之后cerr无法输出 using namespace std; //思路:先创建文件,再写入文件,之后copy文件,最后输出文件 class File { public: void Build(); void Read(); void Copy(); void Write(); }; void File::Build() { int a[10]; ofstream outfile("file.dat", ios::out); if (!outfile) { cerr << "Open error!" << endl; exit(1); } cout << "请输入10个整数" << endl; for (int i = 0; i < 10; i++) { cin >> a[i]; outfile << a[i] << '\t'; } outfile.close(); } void File::Read() { int a[20], Max = 0, Min = 0; ifstream infile("copy_file.dat", ios::in | ios::_Nocreate); if (!infile) { cerr << "Open error!" << endl; exit(1); } for (int i = 0; i < 20; i++) { infile >> a[i]; cout << a[i] << '\t'; } cout << endl; Max = a[0]; Min = a[0]; int sum = 0, count = 0; for (int i = 0; i < 20; i++) { if (Max < a[i]) { Max = a[i]; } if (Min > a[i]) { Min = a[i]; } sum += a[i]; count++; } infile.close(); cout << "Max=" << Max << endl; cout << "Min=" << Min << endl; cout << "平均数为:" << (sum / count) << endl; } void File::Copy() { int swap[20]; ifstream infile("file.dat", ios::in); if (!infile) { cerr << "Open error!" << endl; exit(1); } for (int i = 0; i < 20; i++) { infile >> swap[i]; } infile.close(); ofstream outfile("copy_file.dat", ios::out | ios::_Noreplace); if (!outfile) { cerr << "Open error!" << endl; exit(1); } for (int i = 0; i < 20; i++) { outfile << swap[i] << '\t'; } outfile.close(); } void File::Write() { int a[10]; ofstream outfile("file.dat", ios::app); if (!outfile) { cerr << "Open error!" << endl; exit(1); } cout << "请输入想加入的10个整数" << endl; for (int i = 0; i < 10; i++) { cin >> a[i]; outfile << a[i] << '\t'; } outfile.close(); } int main() { File test; test.Build(); test.Write(); test.Copy(); test.Read(); system("pause"); return 0; }
第二版
#include<fstream> #include<iostream> using namespace std; class File { public: void Write(int sign=0) { if(sign==0) { int temp; ofstream outfile("1.txt",ios::out); cout<<"请输入五个整数:"<<endl; for(int i=0;i<5;i++) { cin>>temp; outfile<<temp<<'\t'; } outfile.close(); } else { int temp; ofstream outfile("1.txt",ios::app); cout<<"请输入想添加的五个整数:"<<endl; for(int i=0;i<5;i++) { cin>>temp; outfile<<temp<<'\t'; } outfile.close(); } } void Read() { int a[10],Max,Min,count=0; float Sum=0; ifstream infile("2.txt",ios::in); for(int i=0;i<10;i++) { infile>>a[i]; cout<<a[i]<<'\t'; } Max=a[0],Min=a[0],Sum=0; for(int i=0;i<10;i++) { if(Max<a[i]) Max=a[i]; if(Min>a[i]) Min=a[i]; Sum+=a[i]; count++; } cout<<"\tMax="<<Max<<"\tMin="<<Min<<"\tAVR="<<Sum/count<<endl; } void Copy() { int swap[10]; ifstream infile("1.txt",ios::in); for(int i=0;i<10;i++) infile>>swap[i]; infile.close(); ofstream outfile("2.txt",ios::out); for(int i=0;i<10;i++) outfile<<swap[i]<<'\t'; outfile.close(); } }; int main() { File test; test.Write(); test.Write(1); test.Copy(); test.Read(); return 0; }
相关文章推荐
- C++向文件中写入数据
- C++ 将数据写入链表,将链表写入文件,再将文件中的内容读出
- [C++]读写txt文件和从txt文件中读取数据写入数组中
- C++ 将数据写入二进制文件后,第二次打开文件修改部分数据,怎么前一次写入的数据被删除了。
- c++:批量把数据写入同一文件夹下不同文件
- C++ 写入数据到.txt文件 从.txt文件读取数据 string字符串分割
- C++ 数据写入文件与读回
- 使用C++将OpenCV中Mat的数据写入二进制文件,用Matlab读出
- C++ 数据写入文件与读回
- C++学习--向txt文件写入数据、读取数据、获取行数以及删除文件
- c++创建BMP文件并写入数据
- C++ 把输出结果写入文件/从文件中读取数据
- 使用C++将OpenCV中Mat的数据写入二进制文件,用Matlab读出
- win2003如何为共享文件所在的磁盘进行配额保障数据正常写入
- MFC/C++ CFile写入文件数据,输出utf-8的文本。(防止中文乱码)
- C++ 将数据写入txt文件WriteFile的使用
- C++ 向文件中写入一组数据
- 使用C++将OpenCV中Mat的数据写入二进制文件,用Matlab读出
- 同步所有已挂载的文件系统。所有缓存中的数据将被立刻写入磁盘
- 使用C++将OpenCV中Mat的数据写入二进制文件,用Matlab读出