0

kindly check the code for error as it is writing the record twice in the file. I think error maybe due to eof() function. I have provided class description too just in case.

class student
{
    int sno;
    char sname[20];
    float fees;
public:
    void input()
    {
        cout << "enter sno,sname and fees \n";
        cin >> sno;
        gets(sname);
        cin >> fees;
    }
    void output()
    {
        cout << sno << "\t" << sname << "\t" << fees << "\n";
    }
    int rsno()
    {
        return sno;
    }
}s;

void add()
{
    ofstream f1;
    f1.open("stu.dat", ios::binary | ios::app);
    s.input();
    f1.write((char*)&s, sizeof(s));
    f1.close();
}
void displayall()
{
    ifstream f2;
    f2.open("stu.dat", ios::binary);
    while (!f2.eof())
    {
        f2.read((char*)&s, sizeof(s));
        s.output();
    }
    f2.close();
}
void main()
{
    add();
    displayall()
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • It does not appear that the code posted will do what you said is happening. Can you post an [mcve]? – NathanOliver Jan 26 '16 at 14:23
  • @NathanOliver i have edited the code showing how i intend to add records and then display them but somehow the last record gets written twice. – sumit sarin Jan 26 '16 at 14:42
  • As is your code should not even compile. Please provide a compile-able example. You should also indent your code correctly. I have done it this tmie to show you how it should look. – NathanOliver Jan 26 '16 at 14:48
  • my code does compile in turboc++ without any error. the only problem i have is mentioned above. – sumit sarin Jan 26 '16 at 15:00
  • also can you specify what have you changed because it appears exactly the same with just changes in my output messages. – sumit sarin Jan 26 '16 at 15:04
  • It shouldn't as you do not have a `;` after `displayall()`. Turbo C++ is an very old compiler and should not be used. There are a number of free compilers out there that are much better and quite a bit more standard compliant. – NathanOliver Jan 26 '16 at 15:05
  • It did change anything except the indentation. If you check the [history](http://stackoverflow.com/posts/35015805/revisions) you can see how it looked vs how it looks now. – NathanOliver Jan 26 '16 at 15:06
  • okkk i may have made that mistake while copying buy anyway could you explain the main problem – sumit sarin Jan 26 '16 at 15:22
  • i agree turboc++ shouldnt be used but thats what we are taught on and asked to use :) – sumit sarin Jan 26 '16 at 15:23

4 Answers4

0

Appears you have not declared an instance of student, so you must do so before calling its methods;

student s;
s.input();

By the way, that code dosen't seem to look like it works at first glance

ZetaRift
  • 332
  • 1
  • 9
  • It dosen't really look like you have declared it, if any compiler errors, please provide them in your question. – ZetaRift Jan 26 '16 at 14:43
  • No compilation errors whatsoever with turboc++. I have declared s just before ending the class declaratio. it is allowed to do it that way :) – sumit sarin Jan 26 '16 at 15:01
0

I think there is too few code to give any statement. Is s a student instance? Are you trying to cast it to char array this way? I think you should use some serialization first.

BTW. It is ugliest formatting I've ever seend - pretty unreadable

0

You have to declare an object/instance of the class student in the function before you can call it. You are incorrectly assuming that by creating the class and giving it an alias, you can now call it using s.input. You have to declare it in the function you're going to use it in. student stu; or s stu; at the start of the void add() function.

Also, since you're using C++, I'd highly advise you to use getline or even fgets() instead of gets since the latter is a C function and it is also depreciated and unsafe to use. If you do, I'd suggest taking a look at Using Cin and Getline together. Handling the newline character is very important if you don't want to mess that up.

Community
  • 1
  • 1
Keno
  • 2,018
  • 1
  • 16
  • 26
  • It all works fine I figured out the problem. It is based on the eof I used. I have answered it myself now you can read it – sumit sarin Jan 27 '16 at 04:18
0

Thanks guys but I figured out the answer. The thing is there is some kind of buffer while using eof() function and that is why I was getting repeated results.