for(int i = 0 ;i < 3 ;i ++)
{
infile.write((char*)&stu[i],sizeof(student));
}
infile.read((char*)stu,sizeof(stu));
for(int i = 0 ;i < 3 ;i ++)
{
cout << "第" << i + 1 << "名学生的学号、姓名、成绩:" << stu[i].num << "," <<
stu[i].name << "," << stu[i].score << endl;
}
put和get和getline和文件流和文件流中的put和get和getline
使用write和read按数据块来进行输入和输出
文件的随机读写,即从特定位置开始读写:seekg()函数和色的seekp()函数分别定位输入文件流对象和输出文件流对象的文件指针,
本节代码
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
struct student
{
char num[10] ;
char name[20] ;
int score ;
};
int main()
{
student stu[3] =
{
{"1210101","张三" ,618},
{"1210102","李四",625},
{"12210103","王五",612}
};
int i ,m;
fstream outfile("studentinfo.dat",ios::binary|ios::out) ;
if(!outfile.is_open())
{
cout << "打开失败" << endl;
return 0 ;
}
for(int i = 0 ;i < 3 ;i ++)
{
outfile.write((char*)&stu[i],sizeof(student));
}
outfile.close() ;
fstream myfile;
myfile.open("studentinfo.dat",ios::binary|ios::in|ios::out);
if(!myfile.is_open())
{
cout << "打开失败" << endl;
return 0 ;
}
stu[1].score = 627 ;
myfile.seekg(1*sizeof(student),ios::beg);
myfile.write((char*)&stu[1],sizeof(student)) ;
cout << "请输入待查询的学生序号(1-3):" << endl;
cin >> m ;
if(m<1||m>3)
{
cout << "该学生不存在" << endl;
return 0 ;
}
myfile.seekg((m-1)*sizeof(student),ios::beg) ;
myfile.read((char*)&stu[m-1],sizeof(student));
cout << "第" << m << "名学生的学号姓名成绩为" << stu[m-1].num << ","<< stu[m-1].name << ","<< stu[m-1].score << endl;
myfile.close() ;
return 0;
}
自定义类型的输入和输出(插入运算符和提取运算符的重载)
本节代码,其中in和out均为流对象代码为
#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
struct student
{
char num[10] ;
char name[20] ;
int score ;
};
istream& operator >>(istream& in,student&stu)
{
in>>stu.num >> stu.name >> stu.score ;
return in ;
}
ostream& operator <<(ostream& out,student&stu)
{
out<<stu.num << " "<<stu.name << " "<<stu.score << endl;
return out ;
}
int main()
{
student stu[3] =
{
{"1210101","张三" ,618},
{"1210102","李四",625},
{"12210103","王五",612}
};
int i;
fstream outfile("studentinfo.dat",ios::binary|ios::out) ;
if(!outfile.is_open())
{
cout << "打开失败" << endl;
return 0 ;
}
for(int i = 0 ;i < 3 ;i ++)
{
outfile<<stu[i] ;
}
outfile.close() ;
fstream infile;
infile.open("studentinfo.dat",ios::binary|ios::in);
if(!infile.is_open())
{
cout << "打开失败" << endl;
return 0 ;
}
for(int i = 0 ;i < 3 ;i ++)
{
infile>>stu[i] ;
}
for(int i = 0 ;i < 3 ;i ++)
{
cout << "第" << i + 1 << "名学生的信息为 :" ;
cout << stu[i] ;
}
infile.close() ;
return 0;
}
还可以为文件流单独编写重载函数,用二进制文件存储学生信息。
fstream& operator<<( fstream& outfile, student& stu)
{
outfile.write((char* )&stu, sizeof(stu));
return outfile;
}
fstream& operator>>( fstream& infile, student& stu)
{
infile.read((char* )&stu, sizeof(stu));
return infile;
}
函数模板格式:template <<模板形参表>><函数类型> <函数名>(<函数形参表>)(返回值为第二个的“函数类型”),编译器会自己识别,通常把函数模板放在头文件中。
#include <iostream>
using namespace std;
//求两个数中较大的数的模板
template <typename T>
T Max(T a,T b)//T为数据类型
{
return (a > b ?a : b);
}
//编写一个程序,其功能是查找一维数组a中第一个值为x的元素的下标。如果不存在该元素则返回-1。
template <typename M>
int Find(M a[],M b ,int n)
{
for(int i = 0 ;i < n ;i ++)
{
if(a[i] == b)
{
return i ;
}
}
return -1 ;
}
int main()
{
int c[6] = {1,2,3,4,5,6} ;
char d[3] = {'a','b','c'} ;
cout << "5在数组c中下标为" << Find(c,5,6) << endl;
cout << "a在数组d中下标为" << Find(d,'a',3) << endl;
int m = 10 ,n = 20 ;
double x = 12.1 ,y = 13.5 ;
char a = 'z' ,b = 'w' ;
cout << Max(1,2) << endl;
cout << Max(x,y) << endl;
cout << Max(a,b) << endl;
return 0;
}
函数模板与函数重载的差别:
重载函数各函数体内可以执行不同的代码,但同-个函数模板实例化的不同模板函数都执行相同的代码,仅仅是处理的数据的类型不同而已。
类模板:
需要自己给模板实例化
#include <iostream>
using namespace std;
//求两个数中较大的数的模板
template <typename T,int length>
class Array
{
public :
T GetElement(int i)
{
return buffer[i] ;
}
int GetLength()
{
return sizeof(buffer) / sizeof(T) ;
}
void SetElement(T) ;
private :
T buffer[length] ;
};
template <typename T,int length>
void Array<T ,length > ::SetElement(T x)
{
for(int i = 0 ;i < GetLength() ;i ++)
{
buffer[i] = x ;
}
}
int main()
{
Array <int ,10> a ;
Array <double , 20> b ;
Array <char ,30> c ;
cout << a.GetLength() << endl;
return 0;
}
数据结构基本概念与术语:
数据>数据元素(结点)>数据项
数据对象:性质想通过的数据元素的集合