无法从文件

时间:2016-08-18 15:18:41

标签: c++ file object header-files

因此,为了实现文件I / O,我做了以下代码。我也做了一个头文件,因为我一次又一次地使用它。我是制作头文件的新手,包括它们所以请指出错误以及如何纠正错误。

这是我的头文件

#ifndef STUDENT_H
#define STUDENT_H


class student
{
    public:
        void getstud();
        void putstud();
        int retroll();
        char* retname();
        int retclass();
        student();
        virtual ~student();
        student(const student& other);
    protected:
    private:
        int roll;
        char name[35];
        int Class;
};

#endif // STUDENT_H

这是相应的cpp文件

#include "student.h"


using namespace std;
void student::getstud()
{
    cout<<"\nenter the roll number : "; cin>>roll;
    cin.clear(); cin.ignore(1);
    cout<<"\nenter the name        : "; cin.getline(name, 35);
    cout<<"\nenter the class       : "; cin>>Class;
}

void student::putstud()
{
    cout<<"\nroll number : "<<roll;
    cout<<"\nname        : "<<name;
    cout<<"\nClass       : "<<Class;
}

int student::retroll() {return roll;}

char* student::retname() {return name;}

int student::retclass() {return Class;}

student::student()
{
    //ctor
}

student::~student()
{
    //dtor
}

student::student(const student& other)
{
    //copy ctor
}

这是程序:

# include <fstream>
# include <conio.h>
# include "student.cpp"

using namespace std;

int main()
{
    student s, s2;
    fstream file;

    file.open("recs.dat", ios::binary);
    cout<<"enter the details of a student :\n";
    s.getstud();
    file.write((char *)&s, sizeof(s));
    file.seekg(0); file.clear();
    file.read((char *)&s2, sizeof(s2));
    s2.putstud();
    s.putstud();
    file.close();
    return 0;
}

输出类似于

enter the details of the student :
enter roll number : 23
enter name        : danny
enter Class       : 12

roll number  : (some number)
name         :
class        : 8
roll number  : 23
name         : danny
class        : 12
什么可能出错?请帮助。

0 个答案:

没有答案