/* 在这个程序中,我的 searchfile 函数有问题。这是一个检查与 fstream 关联的文件是否传递的卷号的函数。是否已经存在于文件中。不幸的是,它正在无限循环中。那么这个问题的原因和解决方法是什么*/
注意:searchfile 正在搜索的文件内容如下:-
234|45|-1|-1|-1|-1|
325|56|-1|-1|-1|-1|
所以每行文件有19个字符。我们将读取卷号。eg.234 那么我们必须跳过 19-4=15 个字符来读取下一个卷号。所以我们必须转到 16 个字符。
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
struct student{
char roll[4];
char name[30];
char branch[20];
};
struct course{
char cid[3];
char name[10];
char credits[2];
};
//buffer class to pack student struct details
class buffer_s{
public:
char buf[200];
void pack(student &s);
};
void buffer_s::pack(student &s){
buf[0]='\0';
strcat(buf,s.roll);
strcat(buf,"|");
strcat(buf,s.name);
strcat(buf,"|");
strcat(buf,s.branch);
strcat(buf,"#");
}
//buffer class to pack course struct details
class buffer_c{
public:
char bufc[200];
void pack(course &c);
};
void buffer_c::pack(course &c){
bufc[0]='\0';
strcpy(bufc,c.cid);
strcat(bufc,"|");
strcat(bufc,c.name);
strcat(bufc,"|");
strcat(bufc,c.credits);
strcat(bufc,"#");
}
//operator overloading for getting input of student struct
istream &operator >> (istream &in,student &s){
cout<<"enter roll no. :";
in.getline(s.roll,4);
cout<<"enter name : ";
in.getline(s.name,30);
cout<<"enter branch : ";
in.getline(s.branch,20);
return in;
}
//operator overloading for getting input of course struct
istream &operator >> (istream &in,course &c){
cout<<"enter course id no: ";
in.getline(c.cid,3);
cout<<"enter course name : ";
in.getline(c.name,10);
cout<<"enter credit ";
in.getline(c.credits,2);
return in;
}
//for searching whether the passed "roll" is already present in the file or not
int searchfile(fstream &fin,char roll[]){
fin.clear();
fin.seekg(0,ios::beg);
char word[4];
int f=0;
if(fin.good())
{
fin.getline(word,3,'|');
word[3]='\0';
while(!fin.eof()){
cout<<"test"<<endl;//for testing the infinite condition of loop
if(!strcmp(roll,word))
{f=1;break;}
else{
fin.seekg(16,ios::cur);//16 since total characters in a line of file 3 is 20.4 already read so 20-4=16.
fin.getline(word,4,'|');
word[3]='\0';
}
}
}
fin.clear();
if(f==1)
return 1;
else
return 0;
}
//for putting things into r_detail file
void fillreg(fstream &file3,student &s,course &c){
file3<<s.roll;
file3<<'|';
file3<<c.cid;
file3<<'|';
file3<<-1;
file3<<'|';
file3<<-1;
file3<<'|';
file3<<-1;
file3<<'|';
file3<<-1;
file3<<'|';
file3<<endl;
}
int main(){
buffer_s s;
buffer_c c;
student ss;
course cs;
fstream file1;
fstream file2;
fstream file3;
file1.open("s_detail.dat");
file2.open("c_detail.dat");
file3.open("r_detail.dat");
int k=0;
//k=2 for taking 2 inputs only.
while(k<2){
file3.clear();
file3.seekg(0,ios::beg);
cout<<"enter student details: "<<endl;
cin>>ss;
cout<<"enter course details: "<<endl;
cin>>cs;
cout<<endl;
int x=searchfile(file3,ss.roll);
cout<<x<<endl;
if(!x)
{
s.pack(ss);
file1.clear();
file1.seekp(0,ios::end);
file1<<s.buf;
file1<<endl;
}
c.pack(cs);
file2.clear();
file2.seekp(0,ios::end);
file2<<c.bufc;
file2<<endl;
file3.clear();
file3.seekp(0,ios::end);
fillreg(file3,ss,cs);
k++;
}
return 0;
}