问题
im working on a console application in C++ (I'm a beginner) and the purpose is to make a small application that can store, read and modify students records
Now, i want to group up data of binary file (class9.dat) such that all the students with common batch id ( char type batch[5] ) should stuck together , by saying "stuck together", i mean if initially, consider binary file consider data in this form
[Tom , 2]
[Harry, 1]
[Peter, 2]
[Stephen, 1]
[Henry , 0]
then, after stucking data together (or grouping them up ) , data should be arranged in file as follow
[Tom , 2]
[Peter, 2]
[Harry, 1]
[Stephen, 1]
[Henry , 0]
(note that the data is arranged in the order in which they are read/encountered first from top-to-bottom)
ok, now i tried myself alot and even i think i found a solution to this problem, i used concept of dynamic linked list (stack) to keep check whether a particular batch ID is arranged properly or not
below is a code snippet for the function void_update()
void update_files()
{
fin.open("class9.dat",ios::in|ios::binary);
fout.open("temp.dat",ios::out|ios::binary);
int total_records,pos,rec,temp;
char temp_batch[5];
fin.seekg(0,ios::beg);
fin.read((char*)&s,sizeof(s));
fin.seekg(0,ios::end);
pos=fin.tellg();
total_records=pos/sizeof(s);
fin.seekg(0,ios::beg);
rec=0;
while(rec<total_records)
{ fin.seekg(rec*sizeof(s),ios::beg);
fin.read((char*)&s,sizeof(s));
strcpy(temp_batch,s.get_char(3));
temp=checklist(temp_batch); //function checks whether batch already in stack or not (either already sorted together or not)
if(temp==0)
{
fin.seekg(0,ios::beg); //move to first record
while(fin.read((char*)&s,sizeof(s)))
{
if(strcmpi(temp_batch,s.get_char(3))==0) //get_char(3) returns student batch id
{
fout.write((char*)&s,sizeof(s));//writting copies of data to temp.dat where they are expected to be grouped together
}
}
push_to_stack(temp_batch);//SEND TEMP_BATCH TO STACK
}
rec++;
}
fout.close();
fin.close();
delete_stacks(); //delete dynamic stack
fin.open("temp.dat",ios::in|ios::binary);
fout.open("class9.dat",ios::out|ios::binary);
while(fin.read((char*)&s,sizeof(s)))//writting sorted data back into class9.dat file from temp.dat
{
fout.write((char*)&s,sizeof(s));
}
fin.close();
fout.close();
}
This is the function code snippet, for your reference im leaving code-snippets of 2 other functions which are belonging to stacks here they are
struct node
{
char batches[5];
node *next;
}*start=NULL,*end=NULL, *temp;
void push_to_stack(char* b)
{
temp=new(node);
if(temp==NULL)
{
cout<<"FATAL ERROR : MEMORY OVERFLOW ";
cout<<"\n YOUR SYSTEM RAN OUT OF MEMORY. PLEASE RESTART THE SYSTEM \n";
cout<<"\n \n TERMINATING STUDENT DATA MANAGER !!!";
getch();
exit(0);
}
strcpy(temp->batches,b);
temp->next=start;
if(end==NULL)
{
end=temp;
}
start=temp;
}
int checklist(char *b)
{
temp=start;
while(temp!=NULL)
{
if(strcmpi(b,temp->batches)==0) //matched
{
return 1;//yes ,BATCH IS EXIsTS
}
else temp=temp->next;
}
return 0;
}
void delete_stacks()
{ node *save;
save=start;
if(save==NULL)
return;
else
while(start!=NULL)
{
save=start;
if(save==NULL)
break;
start=start->next;
delete(save);
}
return;
}
this is it, now let me tell you whats my approach to this problem
what im trying to do is basically i pass student's batch id to another char variable temp_batch , then i check if temp_batch aldready exists in my stack (which contain list of all batch ids on which function is already performed) if temp_batch do not exists in my stack then, it means that the data having batch id as temp_batch is not grouped up , so run a while-loop which read whole file from beg to end and when it found the record having batch id same as temp_batch, it is appended to temp.dat in this manner for every record on file, it is expected to group up records together. but this is not working whats problem is , when i run this code instead of grouping all records , it only GROUPS UP records of that BATCH ID which it encounter first, rest it ignores all other and do not send them to temp.dat example, consider above example, then this is the record which will be sended to temp.dat after whole process
[Tom , 2]
[Peter, 2]
please help me, i did alot of inspection of code , again and again but failed to get the problem... and yes, thanks for reading this whole question upto this point.
EDIT / UPDATE TO QUESTION : some people also asked about "s" in fin/fout statements , here it is well s is object of class student as defined below
class student
{
private:
char name[50],address[100];
char age[5];//this is a replacement
char sub1[80],sub2[80];
int standard;//int class
char id[6];
char mobile[16];
char mobile2[16];
char batch[5]; //this is the batch id im talking about, on basis of which records need to be grouped together
public:
/*1*/
//--------------------------
void getdata(int); //gets data, this is just declaration here
int countname(char *names); // ignore this
int countid(char *sid); //ignore this
void showdata(); //ignore this
void new_data(int);//ignore this
void changeclass(); //ignore this
//below is a function you might be interested in
char *get_char(int code) // 0 (ID) , 1(AGE) , 2 (NAME) , 3(BATCH)
{
if(code==0)
return id; //char
else if(code==1)
return age; //char
else if(code==2)
return name;
else if(code==3)
return batch;
}
//this function below returns student's standard (or class in common language day to day life)
int get_class()
{ return standard;
}
//--------------------------
}s; //end of class , created object s for student
来源:https://stackoverflow.com/questions/36335823/grouping-up-students-records-in-a-binary-file-in-c-based-on-their-batch-number