3
#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;
}

The above program first creates a file "stu.txt" and writes user-given input to the file. It is then supposed to search for a record based on the name the user enters ( using the search() function ). I'm getting errors when calling search() and also defining search(). I've put in the errors the compiler throws as comment lines. Could anyone explain what's going wrong in there ?

4
  • You cannot have a fstream parameter because fstream cannot be copied. Use fstream& instead. Commented Sep 9, 2014 at 5:53
  • Thanks ! That removed the errors ! Commented Sep 9, 2014 at 5:54
  • 1
    Could you explain why fstream& works ? Commented Sep 9, 2014 at 5:55
  • 2
    It passes the stream by reference (look up pass by reference). Commented Sep 9, 2014 at 5:58

1 Answer 1

2

fstream is not copyable, so you have to pass fstream as a reference, or in c++11, move it.

Given you access f after calling search, it is best to pass it by reference.

Change your function to accept the fstream as a reference:

void search(fstream& fin,char* nm)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.