本文共 4378 字,大约阅读时间需要 14 分钟。
系统升级第一步:转换现有数据格式(附:数据文件)
#include用二进制文件完成业务#include #include using namespace std;typedef struct{ int NO; char name[8]; int chinese; int math; int english; int Comprehensive; int total;} Student; //高考学生信息const int maxNum = 10000;void display(Student*, int);int main(){ ifstream asciiFile("asciidata.txt", ios::in); if(!asciiFile) { cerr<<"cannot open ascii file!"< >students[stuNum].NO; asciiFile>>students[stuNum].name; asciiFile>>students[stuNum].chinese; asciiFile>>students[stuNum].math; asciiFile>>students[stuNum].english; asciiFile>>students[stuNum].Comprehensive; asciiFile>>students[stuNum].total; stuNum++; } asciiFile.close(); display(students, stuNum); //写入到二进制文件 int i; ofstream binaryFile("binarydata.dat", ios::out|ios::binary); if(!binaryFile) { cerr<<"cannot open binary file!"<
#include索引文件的建立#include #include using namespace std;typedef struct{ int NO; char name[8]; int chinese; int math; int english; int Comprehensive; int total;} Student; //高考学生信息void display(Student*, int);void sort(Student*, int);int main(){ int stuNum = 0; Student *students; fstream binaryFile("binarydata.dat", ios::in|ios::out|ios::binary); if(!binaryFile) { cerr<<"cannot open binary file!"< s[j+1].NO) { temp=s[j]; s[j]=s[j+1]; s[j+1]=temp; } return;}
#include索引文件的利用#include #include using namespace std;typedef struct{ int NO; char name[8]; int chinese; int math; int english; int Comprehensive; int total;} Student; //高考学生信息typedef struct{ int NO; long offset; //数据在文件中的偏移量} StudentIndex; //高考学生索引void createIndex();void writeIndex(StudentIndex *si, int n);int main(){ createIndex(); return 0;}/*功能:创建索引*/void createIndex(){ int stuNum; StudentIndex *studentsIndex; //索引表的起始地址 Student student; ifstream binaryFile("binarydata.dat", ios::in|ios::binary); if(!binaryFile) { cerr<<"cannot open binary file!"< studentsIndex[j+1].NO) { temp=studentsIndex[j]; studentsIndex[j]=studentsIndex[j+1]; studentsIndex[j+1]=temp; } //将建好的索引表通过文件存储 writeIndex(studentsIndex, stuNum); return;}/*功能:将索引写入文件参数:si - 索引表起始地址;n - 考生人数,索引记录条数*/void writeIndex(StudentIndex *si, int n){ //打开文件 ofstream indexFile("binarydata.idx", ios::out|ios::binary); if(!indexFile) { cerr<<"cannot open index file!"<
#include#include #include using namespace std;typedef struct{ int NO; char name[8]; int chinese; int math; int english; int Comprehensive; int total;} Student; //高考学生信息typedef struct{ int NO; long offset; //数据在文件中的偏移量} StudentIndex; //高考学生索引//为方便起见,下面变量用全局变量表示。若用局部变量,各模块间通过引用传递参数亦可fstream dataFile, indexFile;int stuNum, maxNum;StudentIndex *studentsIndex; //索引表入口地址//函数声明void createIndex();void writeIndex(StudentIndex *si, int n);void init();void work();int chooseInMenu();void done();void displayByIndex();void displayStudent(Student &s);int main(){ char yn; cout<<"需要重建索引吗?(Y/N)"; cin>>yn; if('Y'==yn||'y'==yn) createIndex(); init(); work(); done(); return 0;}/*功能:创建索引*/void createIndex(){ int stuNum; StudentIndex *studentsIndex; //索引表的起始地址 Student student; ifstream binaryFile("binarydata.dat", ios::in|ios::binary); if(!binaryFile) { cerr<<"cannot open binary file!"< studentsIndex[j+1].NO) { temp=studentsIndex[j]; studentsIndex[j]=studentsIndex[j+1]; studentsIndex[j+1]=temp; } //将建好的索引表通过文件存储 writeIndex(studentsIndex, stuNum); return;}/*功能:将索引写入文件参数:si - 索引表起始地址;n - 考生人数,索引记录条数*/void writeIndex(StudentIndex *si, int n){ //打开文件 ofstream indexFile("binarydata.idx", ios::out|ios::binary); if(!indexFile) { cerr<<"cannot open index file!"< >i; if(i>=0 && i<=5) break; else cout<<"请重新选择功能\n"<
转载地址:http://ccntx.baihongyu.com/