您的位置:首页 > 编程语言 > C语言/C++

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: