#include<iostream>
#include<fstream>
#include<string>
#include <conio.h>
usingnamespace std;
int main()
{
fstream io;
char fname[10];
cout<<"Enter the name of the file"<<endl;
cin>>fname;
cout<<"1)Write to a file"<<endl<<"2)Read from a file"<<endl;
int i;cin>>i;
char c='y';
char* memblock;
streampos s;
string sent;
switch (i)
{
case 1:io.open(fname,ios::out|ios::binary);
while(c=='y'||c=='Y')
{
cout<<"Type what you want to write to the file"<<endl;
cin>>sent;
s=sent.length();
memblock=newchar[s];
io.write(memblock,s);
cout<<"Do you want to continue writing"<<endl;
cin>>c;
}
io.close();
delete[] memblock;break;
case 2:
io.open(fname,ios::in|ios::binary|ios::ate);
if (io.is_open())
{
s = io.tellg();
memblock = newchar [s];
io.seekg (0, ios::beg);
io.read (memblock, s);
io.close();
delete[] memblock;
}
else
cout << "Unable to open file";
break;
default:
cout<<"Wrong choice"<<endl;
break;
}
io.close();
getch();
return 0;
}
The thing is,in the console window,when I take the control into case 2,the file shows a blank input.And after I press enter,the application closes,though it wasn't supposed to be so.
Can you explain guys,where Im wrong???
Your views would be gladly taken.