Whats wrong with this code.
I can create the DAT file.Name and Grade is writing to the file perfectly.
but the file contains ASCII values for RollNO and after Grade.

Output file:
Name A ÈB

Program:

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
class stu
{
int rollno;
char name[20];
char grade;
float marks;
public:
void getdata(void)
{
cout<<"enter the roll no\n";
cin>>rollno;
cout<<"enter the name\n";
cin>>name;
cout<<"enter the marks\n";
cin>>marks;
if(marks>=75) grade='A';
else if(marks>=60) grade='B';
else if(marks>=50) grade='C';
else if(marks>=40) grade='D';
else grade='F';
}
void putdata(void)
{
cout<<"RollNo\n"<<rollno;
cout<<"\n Name\n"<<name;
cout<<"\n Marks\n"<<marks;
cout<<"\n Grade\n"<<grade;
}
}s1;
void main()
{
clrscr();
fstream filin;
ofstream filout;
filout.open("test1.dat",ios::out);
{
s1.getdata();
filout.write((char*)&s1,sizeof(s1));
s1.putdata();
filin.read((char*)&s1,sizeof(s1));
}
filin.close();
filout.close();
getch();
}

filin was never opened. I always find it easier to use ifstream instead of fstream for input only strweams.

filin was never opened. I always find it easier to use ifstream instead of fstream for input only strweams.

I tried but couldn't get the result properly.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
class stu
{
int rollno;
char name[20];
char grade;
float marks;
public:
void getdata(void)
{
cout<<"enter the roll no\n";
cin>>rollno;
cout<<"enter the name\n";
cin>>name;
cout<<"enter the marks\n";
cin>>marks;
if(marks>=75) grade='A';
else if(marks>=60) grade='B';
else if(marks>=50) grade='C';
else if(marks>=40) grade='D';
else grade='F';
}
void putdata(void)
{
cout<<"RollNo\n"<<rollno;
cout<<"\n Name\n"<<name;
cout<<"\n Marks\n"<<marks;
cout<<"\n Grade\n"<<grade;
}
}s1;
void main()
{
clrscr();
ifstream filin;
ofstream filout;
{
filout.open("c:\\test1.dat",ios::out | ios::binary);
s1.getdata();
filout.write((char*)(&s1),sizeof(stu));
filout.close();
filin.open("c:\\test1.dat",ios::out | ios::binary);
filin.read((char*)(&s1),sizeof(stu));
s1.putdata();
filin.close();
}
getch();
}

>>filin.open("c:\\test1.dat",ios::out | ios::binary);

use ios::in instead of ios::out. For ifstream you don't have to specify ios::in -- just ios::binary will do.

>>filin.open("c:\\test1.dat",ios::out | ios::binary);

use ios::in instead of ios::out. For ifstream you don't have to specify ios::in -- just ios::binary will do.

No Change in the output.
I am getting the output with some junk characters with the name and grade.

I compiled your program with vc++ 2010 Express and, after making appropriate corrections, it worked ok for me. Here is my version of the program. If you are using Turbo C++ you will have to delete my version of clrscr()

#include<iostream>
#include<conio.h>
#include<fstream>
#include<string>
#pragma warning(disable: 4996)

using std::cin;
using std::cout;
using std::ifstream;
using std::iostream;
using std::ofstream;
using std::ios;
class stu
{
int rollno;
char name[20];
char grade;
float marks;
public:
void getdata(void)
{
cout<<"enter the roll no\n";
cin>>rollno;
cout<<"enter the name\n";
cin>>name;
cout<<"enter the marks\n";
cin>>marks;
if(marks>=75) grade='A';
else if(marks>=60) grade='B';
else if(marks>=50) grade='C';
else if(marks>=40) grade='D';
else grade='F';
}
void putdata(void)
{
cout<<"RollNo\n"<<rollno;
cout<<"\n Name\n"<<name;
cout<<"\n Marks\n"<<marks;
cout<<"\n Grade\n"<<grade;
}
}s1;


void clrscr()
{
    system("cls");
}

int main()
{
clrscr();
ifstream filin;
ofstream filout;
filout.open("c:\\test1.dat", ios::binary);
s1.getdata();
filout.write((char*)(&s1),sizeof(stu));
filout.close();
filin.open("c:\\test1.dat",ios::binary);
filin.read((char*)(&s1),sizeof(stu));
s1.putdata();
filin.close();
getch();
}

Thanks for your efforts but I couldn't get the right result in the file.It continues with the junk character at the end.giving null for rollno.

I am using Turbo C++ 3.0 and it doesn't accept the headers like #include<iostream>.
I used the headers like adding #include<iostream.h>.
I am getting Declaration Syntax Error while using "Using cin::std"

Here is the code I used.Please find the commented lines starts with //.

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#pragma warning(disable: 4996)

//using cin std;                 
//using std::cout;
//using std::ifstream;
//using std::iostream;
//using std::ofstream;
//using std::ios;
class stu
{
int rollno;
char name[20];
char grade;
float marks;
public:
void getdata(void)
{
cout<<"enter the roll no\n";
cin>>rollno;
cout<<"enter the name\n";
cin>>name;
cout<<"enter the marks\n";
cin>>marks;
if(marks>=75) grade='A';
else if(marks>=60) grade='B';
else if(marks>=50) grade='C';
else if(marks>=40) grade='D';
else grade='F';
}
void putdata(void)
{
cout<<"RollNo\n"<<rollno;
cout<<"\n Name\n"<<name;
cout<<"\n Marks\n"<<marks;
cout<<"\n Grade\n"<<grade;
}
}s1;

//void clrscr()
//{
//  system("cls");
    // }

int main()
{
clrscr();
ifstream filin;
ofstream filout;
filout.open("c:\\test1.dat", ios::binary);
s1.getdata();
filout.write((char*)(&s1),sizeof(stu));
filout.close();
filin.open("c:\\test1.dat",ios::binary);
filin.read((char*)(&s1),sizeof(stu));
s1.putdata();
filin.close();
getch();
}

Yes, your compiler is very very old, written before c++ changed the standards to use headers without the .h extension and before std::

Please post a example inputs that you entered when you ran your program.

Yes, your compiler is very very old, written before c++ changed the standards to use headers without the .h extension and before std::

Please post a example inputs that you entered when you ran your program.

Input

rollno 123
name studenta
marks 90

Expected output

123 studenta A

Now I am receiving only name as correct,rollno has been replaced with Ascii values and grade appended wth ascii values.

One more question.
How to create a DAT file.
I just downloaded DEV C++ IDE and used your code.
The file created as same before.
Now I got a doubt how to create a DAT file.Is it thorugh program or by DOSprompt?


OUTPUT file:
sa F B

>>OUTPUT file:

Oh -- you can not view the output file with a text editor such as Notepad because the outpout file is in binary, not ascii. Only your program can read the binary file. If the last few lines of your program outputs the correct value (which is does for me) then there is nothing wrong with your program.

>>OUTPUT file:

Oh -- you can not view the output file with a text editor such as Notepad because the outpout file is in binary, not ascii. Only your program can read the binary file. If the last few lines of your program outputs the correct value (which is does for me) then there is nothing wrong with your program.

Thanks for your time Ancient Dragon. I got it.So DAT files cannot be read from a text editor and it can be read through program only.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.