Thread: Passing Class object to the same class method

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    India
    Posts
    53

    Passing Class object to the same class method

    Hi folks, I have written code to random access files in c++ . I got result without creating a method for write a record to the file. I have given the code snippet below.
    Code:
    #include<iostream>
    
    #include<fstream>
    #include<iomanip>
    using namespace std;
    class student
    {
     int rollno,marks;
     char name[20];
     fstream fpointer;
     
     public:
        void show();
        void get();
    
        
    };
    void student::get()
    {
     cout<<"Enter the name of student:";
     cin>>name;
     cout<<"Enter the roll no. of student:";
     cin>>rollno;
     cout<<"Enter the marks of student:";
     cin>>marks;
    }
    int main()
    {
     student record;
     fstream file;
     file.open("student.txt",ios::in|ios::out|ios::binary|ios::app);
     char flag='y';
     int i=0;
     while(flag=='y'||flag=='Y')
     {
        record.get();
        file.seekp(i*sizeof(record), ios::beg);
        file.write((char*)&record,sizeof record);
        cout<<"Enter another record(Y/N)....";
        cin>>flag;
        i++;
     }
     //file.seekg(0);
     cout<<"Roll No.\tName\tMarks"<<endl;
     file.seekp(2*sizeof(record), ios::beg);
     while(file.read((char*)&record,sizeof record))
     {
        record.show();
     }
     file.close();
     return 0;
    }
    void student::show()
    {
     cout<<rollno<<"\t\t"<<name<<"\t"<<marks<<endl;
    }
    But now i created a method inside the class to write and read the record. This is time i could not write and read successfully. What is the problem here. Please tell me.
    Code:
    #include<iostream>
    
    #include<fstream>
    #include<iomanip>
    using namespace std;
    class student
    {
     int rollno,marks;
     char name[20];
     
     
     public:
        void show();
        void get();
        int write(const student &,fstream &,int);
        int read(const student &,fstream &,int);
    };
    void student::get()
    {
     cout<<"Enter the name of student:";
     cin>>name;
     cout<<"Enter the roll no. of student:";
     cin>>rollno;
     cout<<"Enter the marks of student:";
     cin>>marks;
    }
    int student::write(const student &s,fstream &fp,int RecNum)
    {
        if( fp.seekp(RecNum*sizeof(s), ios::beg) == 0 )
            if(fp.write((char*)&s,sizeof s))
                return 1;
        return 0;
    }
    int student::read(const student &s,fstream &fp,int RecNum)
    {
        if( fp.seekg(RecNum*sizeof(s), ios::beg) == 0 )
            if(fp.read((char*)&s,sizeof s))
                return 1;
        return 0;
    }
    int main()
    {
     student record;
     fstream file;
     file.open("student.txt",ios::in|ios::out|ios::binary|ios::app);
     char flag='y';
     int i=0;
     
        record.get();
        record.write(record,file,2);
        
        
     
    
     cout<<"Roll No.\tName\tMarks"<<endl;
     
     record.read(record,file,2);
    
     file.close();
     return 0;
    }
    void student::show()
    {
     cout<<rollno<<"\t\t"<<name<<"\t"<<marks<<endl;
    }

  2. #2
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Live 45 should be somethng like
    Code:
     file.open("student.txt",ios::in|ios::out|ios::binary|ios::app);
     if( !file.is_open() ){
    	 //Handle error here
    	 __asm nop;
     }
    Line 29: if( fp.seekp(RecNum*sizeof(s), ios::beg) == 0 )
    This is not the correct way to check for errors.
    try
    Code:
    	fp.seekp(RecNum*sizeof(s), ios::beg);
    	if( fp.good() ){
            if(fp.write((char*)&s,sizeof s))
                return 1;
    	}else{
    		//Handle error here
    	}
        return 0;
    Your error checking of seekg is incorrect as well.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Instead of checking for success, check for errors. That way, you don't need huge nested if statements which Microsoft is so fond of.
    Code:
    fp.seekp(RecNum*sizeof(s), ios::beg);
    if (!fp)
    {
    	// Handle error here
    	return 0;
    }
    return (fp.write((char*)&s, sizeof(s)) > 0);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Code:
    return (fp.write((char*)&s, sizeof(s)) > 0);
    If that works, it's not what you want. write returns a reference to the stream.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It does not make sense to have member functions that take another instance of the same class and ignore the 'this' pointer.
    Either make the method static or do it properly by removing the const student &s and using the 'this' pts instead.
    You should also ideally do proper serialisation rather than dumping the binary of the object to file, before it bites you later.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by whiteflags View Post
    Code:
    return (fp.write((char*)&s, sizeof(s)) > 0);
    If that works, it's not what you want. write returns a reference to the stream.
    I guess that backfired, huh? I just made a quick re-write of that function.
    Oh well, just learn the advice; don't copy my sloppy re-coding
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 05-22-2009, 11:12 PM
  2. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  3. Passing an array of a class object into a function
    By maoqiu in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2007, 08:42 AM
  4. Replies: 3
    Last Post: 02-12-2002, 10:09 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM

Tags for this Thread