Data structure Practice-index file

Source: Internet
Author: User
Tags rewind

This article is intended for practical projects in the basic series of data Structures (11): Files.

"Project" Index file
There are a number of students whose results are as follows, save this data in the St array:

  学号     姓名    年龄 性别 语文 数学 英语    1      陈华    20   男   78   90   84    5      张明    21   男   78   68   92    8      王英    20   女   86   81   86    3      刘丽    21   女   78   92   88    2      许可    20   男   80   83   78    4      陈军    20   男   78   88   82    7      马胜    21   男   56   67   75    6      曾强    20   男   78   89   82

Based on these data, the program implements the following functions:
(1) Write the St array student record to the Stud.dat file as the master file
(2) The student record in the output master file
(3) Establish an index file corresponding to the master file, where each record consists of two fields: the number of students and the amount of displacement that the student records in the data file (example: The student license for the data Secondary number 2 is listed above, with a displacement of 5. Because this example uses a fixed-length file, you can calculate the relative address of the record in the main file based on the displacement amount. Index files are ordered by number of studies.
(4) All records in the output index file
(5) According to the user input number, using the index file, the binary search method to find the corresponding record number, and then output the record through the main file
(6) Is there an idea to expand the project? Increase the amount of data, hash files, multi-keyword files ...

[Reference Solution]

#include <stdio.h>#include <string.h>#include <malloc.h>#define MAXREC //maximum number of records typedef structIndex//Define index file structure{intNo//Study No.    LongOffset//record number in the master file} Index;typedef struct{intNo//Study No.    Charname[Ten];//Name    intAge//Age    Charsex[3];//Gender    intDEG1,DEG2,DEG3;//Course 1-Course 3 results} Studtype;voidInsertsort (Index r[],intN//Use the direct insertion sort method to r[0..n-1] by the number increment order{intI,j; Index temp; for(i=1; i<n;        i++) {Temp=r[i]; j=i-1; while(j>=0&& temp.no<r[j].no) {r[j+1]=R[J];//Move a record with a keyword greater than R[i].keyj--; } r[j+1]=temp;//Insert r[i at j+1]}}voidCreatidxfile ()//Build index file{FILE *mfile,*idxfile;    Index Idx[maxrec]; Studtype St;intn=0Iif((Mfile=fopen ("Stud.dat","RB")) ==null) {printf("Tip: Cannot open main file \ n");return; }if((Idxfile=fopen ("Index.dat","WB")) ==null) {printf("Tip: Cannot create index file \ n");return; } i=0; while(Fread (&st,sizeof(Studtype),1, Mfile))) {idx[i].no=st.no;        Idx[i].offset=++n;    i++; } insertsort (Idx,n);//Sort the IDX array by no valueRewind (Idxfile); for(i=0; i<n; i++) fwrite (&idx[i],sizeof(Index),1, idxfile);    Fclose (Mfile); Fclose (Idxfile);printf("Tip: Index file is complete \ n");}voidOutputmainfile ()//Output Master file all records{FILE *mfile; Studtype St;intI=1;if((Mfile=fopen ("Stud.dat","RB")) ==null) {printf("Hint: cannot read main file \ n");return; }printf("----Student grades----\ n");printf("Record number study number name age gender language Math english \ n"); while(Fread (&st,sizeof(Studtype),1, mfile)) = =1)    {printf("%6d%5d%10s%6d%5s%5d%5d%5d\n", I,ST.NO,ST.NAME,ST.AGE,ST.SEX,ST.DEG1,ST.DEG2,ST.DEG3);    i++; } fclose (Mfile);}voidOutputidxfile ()//Output index file all records{FILE *idxfile; Index Irec;printf("----Student Index Table----\ n");printf("\ T School number record number \ n");if((Idxfile=fopen ("Index.dat","RB")) ==null) {printf("Hint: cannot read index file \ n");return; } while(Fread (&irec,sizeof(Index),1, idxfile)) = =1)printf("\t%5d%6ld\n", Irec.no,irec.offset); Fclose (idxfile);}voidReadindexfile (Index Idx[maxrec],int&n)//Read index file data stored in IDX array{intJ FILE *idxfile;if((Idxfile=fopen ("Index.dat","RB")) ==null) {printf("Tip: Index file cannot be opened \ n");return; } fseek (Idxfile,0,2); J=ftell (Idxfile);//j Finding the file lengthRewind (Idxfile); n=j/sizeof(Index);//n to find the number of records in a fileFread (IDX,sizeof(Index), n,idxfile); Fclose (idxfile);}intSearchnum (Index idx[],intNintNo//In index file containing N records to find the record number of record number no{intmid,low=0, high=n-1; while(Low<=high)//Two points find{mid= (Low+high)/2;if(Idx[mid].no>no) high=mid-1;Else if(Idx[mid].no<no) low=mid+1;Else    //idx[mid].no==no            returnIdx[mid].offset; }return-1;}voidFindstudent ()//Output a record of the specified school number{intNo    FILE *mfile;    Index Idx[maxrec]; Studtype St;intI,n;if((Mfile=fopen ("Stud.dat","rb+")) ==null) {printf("Tip: There are no records in the main file \ n");return; } readindexfile (Idx,n);//Read indexed array idx    printf("Enter the study number:");scanf("%d", &no); I=searchnum (Idx,n,no);//Find in IDX    if(i==-1)printf("Hint: study number%d does not exist \ n", no);Else{fseek (Mfile, (i-1)*sizeof(Studtype), seek_set);//jump directly to the corresponding record in the main file by the record numberFread (&st,sizeof(Studtype),1, mfile);printf("%5d%10s%6d%5s%5d%5d%5d\n", ST.NO,ST.NAME,ST.AGE,ST.SEX,ST.DEG1,ST.DEG2,ST.DEG3); } fclose (Mfile);}voidWriteFile (Studtype st[],intN//Writes n student records in the St array to the Stud.dat file{intI FILE *FP;if((Fp=fopen ("Stud.dat","WB")) ==null) {printf("\ t hint: cannot create Stud.dat file \ n");return; } for(i=0; i<n; i++) fwrite (&st[i],1,sizeof(Studtype), FP); Fclose (FP);printf("Hint: file Stud.dat created \ n");}intMain () {intn=8, SEL;//n for the actual number of studentsStudtype st[]= {{1,"Chen Hua", -,"Male", +, -, -},        {5,"Zhang Ming", +,"Male", +, the, the},        {8,"Wang Ying", -,"female", the,Bayi, the},        {3,"Liu Li", +,"female", +, the, the},        {2,"License", -,"Male", the, the, +},        {4,"Chen June", -,"Male", +, the, the},        {7,"Maxim Trader", +,"Male", About, the, the},        {6,"Zeng Qiang", -,"Male", +, the, the}    };printf("Create a master file \ n"); WriteFile (St,n);//Establish Master file     Do{printf("1: Output main file 2: Build index file 3: Output index file 4: Find by number 0: exit:");scanf("%d", &sel);Switch(SEL) { Case 1: Outputmainfile (); Break; Case 2: Creatidxfile (); Break; Case 3: Outputidxfile (); Break; Case 4: Findstudent (); Break; }    } while(sel!=0);return 0;}

Data structure Practice-index file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.