-
#include <iostream>
#include <fstream>
#include <string>
using namespace std;const int MAXSIZE = 10;
class StudentRecord
{
private:
int sno;
string sname;
float score;
public:
StudentRecord() {;}
StudentRecord(int no, string name, float s) {sno=no, sname=name, score=s;}void setSno(int no) {sno = no;}
void setSname(string name) {sname = name;}
void setScore(float s) {score = s;}int getSno() const {return sno;}
string getSname() const {return sname;}
float getScore() const {return score;}void readFromFile(ifstream&);
void printRecord(ostream&);
bool operator== (StudentRecord s);
};bool StudentRecord::operator== (StudentRecord s)
{
if(this->sno==s.sno && this->sname==s.sname && this->score==s.score)
return true;
else
return false;
}
void StudentRecord::readFromFile(ifstream& Bin)// 이진파일 읽기
{
// 채워야 할 부분
int len;
char* name = new char[20];
Bin.read((char*) &sno, sizeof(int));
Bin.read((char*) &len, sizeof(int));
Bin.read((char*) name, (std::streamsize)len);
name[len] = '\0';
sname = name;
Bin.read((char*) &score, sizeof(float));
}void StudentRecord::printRecord(ostream&)// 화면에 출력
{
// 채워야 할 부분
cout << sno <<'\t'<< sname <<'\t'<< score <<endl;
}int main(int argc, char* argv[])
{
StudentRecord first, second;ifstream Binfirst(argv[1]);
ifstream Binsecond(argv[2]);if(!Binfirst.is_open())
{
cout << "파일을 열수 없습니다. " << endl;
return 0;
}int findex = 0, sindex = 0;
cout << "first file " << endl;
while (Binfirst.peek() != EOF)
{
first.readFromFile(Binfirst);
first.printRecord(cout);
findex++;
}cout << endl << "second file" << endl;
while (Binsecond.peek() != EOF)
{
second.readFromFile(Binsecond);
second.printRecord(cout);
sindex++;
}
Binfirst.clear();
Binsecond.clear();
Binfirst.seekg(0, ios::beg);
Binsecond.seekg(0, ios::beg);bool bc = true;
//레코드 수검사
if(findex != sindex)
{
cout << "\n\n 파일이 다릅니다.\n\n" << endl;Binfirst.clear();
Binsecond.clear();
Binfirst.seekg(0, ios::beg);
Binsecond.seekg(0, ios::beg);cout << "first file " << endl;
while (Binfirst.peek() != EOF)
{
first.readFromFile(Binfirst);
first.printRecord(cout);
}cout << endl << "second file" << endl;
while (Binsecond.peek() != EOF)
{
second.readFromFile(Binsecond);
second.printRecord(cout);
}
return 0;
}
// 레코드가 같으지 검사
while (Binfirst.peek() != EOF)
{
first.readFromFile(Binfirst);
while(Binsecond.peek() != EOF)
{
second.readFromFile(Binsecond);
if(first==second)
bc = true;
else
{
bc = false;
}
}
}//결과 출력
if(bc==true)
cout << " 파일이 같습니다." << endl;
else
{
cout << " 파일이 다릅니다." << endl;Binfirst.clear();
Binsecond.clear();
Binfirst.seekg(0, ios::beg);
Binsecond.seekg(0, ios::beg);cout << "first file " << endl;
while (Binfirst.peek() != EOF)
{
first.readFromFile(Binfirst);
first.printRecord(cout);
}cout << endl << "second file" << endl;
while (Binsecond.peek() != EOF)
{
second.readFromFile(Binsecond);
second.printRecord(cout);
}
return 0;
}Binfirst.close();
Binsecond.close();return 0;
}