#include <iostream>
#include <fstream>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
class Student{
private:
char name[40];
char grade;
float marks;
public:
void getdata();
void display();
char* getname(){return name;}
void search(fstream,char*);
};
void Student::getdata(){
char ch;
cin.get(ch);
cout<<"Enter name : ";
cin.getline(name,40);
cout<<"Enter grade : ";
cin>>grade;
cout<<"Enter marks : ";
cin>>marks;
cout<<"\n";
}
void Student::display(){
cout<<"Name : "<<name<<"\t";
cout<<"Grade : "<<grade<<"\t";
cout<<"Marks : "<<marks<<"\t"<<"\n";
}
void search(fstream fin,char* nm)/*initializing argument 1 of 'void search(std::fstream, char*)'*/{
Student s;
fin.open("stu.txt",ios::in|ios::binary);
while(!fin){
fin.read((char*)&s,sizeof(s));
if(s.getname()==nm){
cout<<"Record found !";
s.display();
break;
}
}
fin.close();
}
int main(){
system("cls");
char nam[40];
Student arts[3];
fstream f;
f.open("stu.txt",ios::in|ios::out|ios::binary);
if(!f){
cerr<<"Cannot open file !";
return 1;
}
for(int i=0;i<3;i++){
arts[i].getdata();
f.write((char*)&arts[i],sizeof(arts[i]));
}
f.close();
cout<<"Enter name to be searched for : ";
cin.getline(nam,40);
char* p = new char[40];
p=nam;
search(f,p);/*synthesized method 'std::basic_ios<char>::basic_ios(const std::basic_ios<char>&)' first required here*/
getch();
f.close();
return 0;
}
위 프로그램은 먼저 "stu.txt"파일을 만들고 사용자가 입력 한 내용을 파일에 씁니다. 그런 다음 사용자가 입력 한 이름 (search () 함수 사용)을 기반으로 레코드를 검색해야합니다. search ()를 호출하고 search ()를 정의 할 때 오류가 발생합니다. 컴파일러가 주석 줄로 던지는 오류를 입력했습니다. 아무도 거기에서 무엇이 잘못되고 있는지 설명 할 수 있습니까?
fstream
복사 할 수 없으므로 fstream
참조 로 전달 하거나 C ++ 11에서 이동해야합니다.
f
을 호출 한 후 액세스 권한이 주어지면 search
참조로 전달하는 것이 가장 좋습니다.
를 허용하도록 기능을 변경 fstream
A와 참조 :
void search(fstream& fin,char* nm)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다