reading and writing the file

#include<stdio.h>
#include<iostream>
using namespace std;
#include<fstream>

struct employee {

char name[25];
int marks;
float stipend;

};


int main()
{
employee e[2];
for(int i=0;i<2;i++)
{
cout<<"please enter name"<<endl;
cin>>e[i].name;
cout<<"enter marks"<<endl;
cin>>e[i].marks;
cout<<"enter stipend"<<endl;
cin>>e[i].stipend;


}
for(int x=0;x<2;x++)
{
cout<<" name marks and stipend are"<<e[x].name<<e[x].marks<<e[x].stipend<<endl;
}
fstream fileobj;
fileobj.open("myfile",ios::in|ios::out);

for (int k=0;k<2;k++)
{
fileobj.write((char*)(&e[k]),sizeof(e[k]));
}


cout<<" reading from file"<<endl;
char ch;
while(fileobj)
{ fileobj.get(ch);
cout<<ch;
}
employee es;
for(int j=0;j<2;j++)
{
fileobj.read((char*)(&es),sizeof(es));
cout<<es.name<<endl;
cout<<es.marks<<endl;
cout<<es.stipend<<endl;

}
fileobj.close();
return 0;

}

here i wrote this program which gives this output

please enter name
venkat
enter marks
56
enter stipend
7666
please enter name
prasad
enter marks
454
enter stipend
7684
name marks and stipend arevenkat567666
name marks and stipend areprasad4547684
reading from file

-1081492972
-1.07599

-1081492972
-1.07599


can someone help as to why its not wrting and reading into the file exatly..??
I am new to c++ please help


THanks
Venkata prasad manne


Topic archived. No new replies allowed.