Yeah I tried that. it doesn't work.That's a sample program from some c++ site.I wrote this.
Code:
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<conio.h>
void main()
{char c[20];
int i;
clrscr();
cout<<"\nPress 1 to write to the file, 2 to read.\n";
cin>>i;
if(i==1)
{
ofstream fout;
fout.open("name.dat",ios::binary|ios::ate);
cout<<"\nEnter the name.\n";
cin>>c;
fout.write((char*)&c,sizeof(c));
fout.close();
}
if(i==2)
{
ifstream fin;;
fin.open("name.dat",ios::binary|ios::in);
cout<<"\nDisplaying names.";
fin.seekg(20,ios::beg);
fin.read((char*)&c,sizeof(c));
cout<<c;
}
getch();
}
The file has two names and I want to read the 2nd one, so I used fin.seekg(20,ios::beg).. which makes it crash.If I remove it, it works fine , but reads the first name.