//1.cpp
#include<fstream>
#include<iostream>
using namespace std;
int main01()
{
int a[10];
ofstream outfile("f1.dat",ios::out);
if (!outfile)
{
cerr << "open error!"<<endl;
exit(1);
}
cout << "enter 10 integer number:" << endl;
for (int i = 0; i < 10; i++)
{
cin >> a[i];
outfile << a[i] << " ";
}
outfile.close();
int max=a[0], i, order;
ifstream infile("f1.dat",ios::in);
if (!infile)
{
cerr << "open error!" << endl;
exit(1);
}
for (i = 0; i < 10; i++)
{
if (a[i]>max)
max = a[i];
order = i;
}
cout << "the max: " << max << "the order: " << order+1 << endl;
infile.close();
system("pause");
return 0;
}
//2.cpp
#include<fstream>
#include<iostream>
using namespace std;
void save_to_file2()//先存到f2.dat中
{
ofstream outfile("f2.dat", ios::out);
if (!outfile)
{
cerr << "open f2.dat error" << endl;
exit(1);
}
char c[800];
cin.getline(c, 100);
for (int i = 0; c[i] != 0; i++)
{
if (c[i] >= 65 && c[i] <= 90 || c[i] >= 97 && c[i] <= 122)
{
outfile.put(c[i]);
cout << c[i];
}
}
cout << endl;
outfile.close();
}
void get_from_f2()
{
char ch;
ifstream infile("f2.dat", ios::in);//输入方式打开d2.dat
if (!infile)
{
cerr << "open f2.dat error" << endl;
exit(1);
}
ofstream outfile("f3.dat",ios::out);//输出方式打开d3.dat
if (!outfile)
{
cerr << "open f3.dat error" << endl;
exit(1);
}
while (infile.get(ch))//转换并写入
{
if (ch >= 97 && ch <= 122)
ch = ch - 32;
outfile.put(ch);//写入
cout << ch;
}
cout << endl;
infile.close();//关闭
outfile.close();//关闭
}
int main02()
{
save_to_file2();
get_from_f2();
system("pause");
return 0;
}
//3.cpp
#include<fstream>
#include<iostream>
using namespace std;
struct student
{
char name[20];
int num;
int age;
char sex;
};
int main03()
{
student a[3] = { "li", 101, 20, 'm', "ali", 102, 21, 'm', "bli", 103, 22, 'w' };
ofstream outfile("a.dat",ios::binary);
if (!outfile)
{
cerr << "error" << endl;
exit(1);
}
for (int i = 0; i < 3; i++)
{
outfile.write((char*)&a[i],sizeof(a[i]));
}
outfile.close();
ifstream infile("a.dat", ios::binary);
if (!infile)
{
cerr << "a.dat.error" << endl;
exit(1);
}
/*
ofstream outfile("b.dat", ios::binary);
if (!outfile)
{
cerr << "b.dat.error" << endl;
exit(1);
}*/
for (int i = 0; i < 3; i++)
infile.read((char*)&a[i],sizeof(a[i]));//将文件读到程序中
infile.close();
for (int i = 0; i < 3; i++)
{
cout << "no:" << i + 1 << endl;
cout << "name:" << a[i].name << endl;
cout << "num:" << a[i].num << endl;
cout << "age:" << a[i].age<< endl;
cout << "sex:" << a[i].sex << endl;
}
system("pause");
return 0;
}
//4.cpp
#define _CRT_SECURE_NO_WARNINGS
#include<fstream>
#include<iostream>
using namespace std;
struct student
{
char name[20];
int num;
int age;
};
int main04()
{//保存文件到student.dat中///写
student s[3] = { "li", 101, 20, "ali", 102, 21, "bli", 103, 22 };
ofstream outfile("student.dat",ios::binary);
if (!outfile)
{
cout << "student open error" << endl;
exit(0);
}
for (int i = 0; i < 3; i++)
{
outfile.write((char*)&s[i], sizeof(s[i]));
}
outfile.close();
//读取1和3 的信息
ifstream infile("student.dat", ios::binary);
if (!infile)
{
cout << "student open error" << endl;
exit(0);
}
for (int i = 0; i < 3; i = i + 2)
{
infile.seekg(i*sizeof(s[i]), ios::beg);
infile.read((char*)&s[i/2],sizeof(s[0]));
cout << s[i / 2].num << " " << s[i / 2].name << " " << s[i / 2].age << " " << endl;
}
infile.close();//
ofstream outfile1("student.dat", ios::binary);//写S【1】
if (!outfile1)
{
cout << "student open error" << endl;
exit(0);
}
cout << endl;
s[1].num = 10000;
strcpy(s[1].name , "jaosgh");
s[1].age = 100;
outfile1.seekp(0, ios::beg);
outfile1.write((char*)&s[0], sizeof(s[0]));
outfile1.seekp(1* sizeof(s[0]), ios::beg);
outfile1.write((char*)&s[1], sizeof(s[1]));//s[0]==0,0
outfile1.close();
ifstream infile1("student.dat", ios::binary);
if (!infile1)
{
cout << "student open error" << endl;
exit(0);
}
infile.seekg(0,ios::beg);//回到初始位置
for (int i = 0; i < 3; i++)
{
infile1.read((char*)&s[i], sizeof(s[i]));
cout << s[i ].num << " " << s[i].name << " " << s[i ].age << " " << endl;
}
infile1.close();//
system("pause");
return 0;
}
//5.cpp
#include<fstream>
#include<iostream>
using namespace std;
int main05()
{ //20个数,前一半放ST1.后一半放ST2
int st[20],st1[10],st2[10];
cout << "please enter ten ingers :" << endl;
for (int i = 0; i < 20; i++)
{
cin >> st[i];
}
ofstream outfile("f15.dat",ios::out);
if (!outfile)
{
cerr << "open f15.dat error" << endl;
exit(0);
}
for (int i = 0; i < 10; i++)
{
st1[i] = st[i];
outfile << st1[i] << " ";
cout << st1[i] << " ";
}
cout << endl;
outfile.close();
ofstream outfile1("f16.dat", ios::out);
if (!outfile1)
{
cerr << "open f16.dat error" << endl;
exit(0);
}
for (int i = 0; i < 10; i++)
{
st2[i] = st[i+10];
outfile1 << st2[i] << " ";//把st2写进去
cout << st2[i] << " ";
}
cout << endl;
outfile1.close();
for (int i = 0; i < 10; i++)
{
cout << st1[i] << " ";
}
for (int i = 0; i < 10; i++)
{
cout << st2[i] << " ";
}
cout << endl;
//将ST1写到ST2后面去
ifstream infile("f15.dat",ios::out);
if (!infile)
{
cerr<<"open f15.error" << endl;
exit(0);
}
ofstream outfile2("f16.dat", ios::app);
if (!outfile2)
{
cerr << "open f16.dat error" << endl;
exit(0);
}
for (int i = 0; i < 10; i++)
{
st2[10 + i] = st1[i]; //此处文件写需要数组写同步;此处不写文件可写入,但读文件的时候会有问题。
outfile2 << st1[i] << " ";
}
cout << endl;
outfile2.close();
infile.close();
ifstream infile1("f16.dat", ios::out);
if (!infile1)
{
cerr << "open f16.dat error" << endl;
exit(0);
}
for (int i = 0; i < 20; i++)
{
cout << st2[i] << " ";
}
infile1.close();
cout << endl;
//将ST2排序输出
ifstream infile2("f16.dat", ios::out);
if (!infile2)
{
cerr << "open f16.dat error" << endl;
exit(0);
}
ofstream outfile3("f17.dat", ios::out);
if (!outfile3)
{
cerr << "open f17.dat error" << endl;
exit(0);
}
for (int i = 0; i < 20; i++)
for (int j = i + 1; j < 20 ; j++)
{
if (st2[i]>st2[j])
{
int temp;
temp = st2[i];
st2[i] = st2[j];
st2[j] = temp;
}
}
for (int i = 0; i < 20; i++)
{
outfile3<< st2[i] << " ";
cout << st2[i] << " ";
}
outfile3.close();
infile2. close();
system("pause");
return 0;
}
//6.cpp
#include<iostream>
#include<fstream>
using namespace std;
struct pl
{
int num;
char name[20];
int age;
float salary;
};
//istream& operator>>(istream&input, pl&p)
//{
// input >> p.num >> p.name[20]>> p.age >> p.salary;
// return input;
//}
//ostream& operator>>(ostream&output, pl&p)
//{
// return output;
//}
int main()
{
pl p5[5] = { 1001, "zhao", 17, 100, 1002, "qian", 18, 101 ,1002, "sun", 19, 102 ,1003, "li", 20, 103 ,1004, "zhou", 21, 104 };
ofstream outfile("f6.dat",ios::binary);
if (!outfile)
{
cout << "f6.dat open error" << endl;
exit(0);
}
for (int i = 0; i < 5;i++)
outfile.write((char*)&p5[i], sizeof(p5[i]));
outfile.close();
ofstream outfile1("f6.dat", ios::binary|ios::app);
if (!outfile1)
{
cout << "f6.dat open error" << endl;
exit(0);
}
cout << " 键入信息 " << endl;
pl p2[2];
/*for (int i = 0; i < 2; i++)
{
cin >> p2[i];
cout << p2[i].num << p2[i].name << p2[i].age << p2[i].salary << endl;
}//运算符重载;*/
for (int i = 0; i < 2; i++)
{
cin >> p2[i].num >> p2[i].name >> p2[i].age >> p2[i].salary;
}
for (int i = 0; i < 2; i++)
{
p5[i + 5] = p2[i];
outfile1.write((char*)&p5[i + 5], sizeof(p5[i + 5]));
}
outfile1.close();
ifstream infile("f6.dat",ios::binary);
for (int i = 0; i < 7; i++)
{
infile.read((char*)&p5[i], sizeof(p5[i]));
cout << p5[i].num << " " << p5[i].name << " " << p5[i].age << " " << p5[i].salary << endl;
}
infile.close();
system("pause");
return 0;
}