Input from a file not working...

Well guys Im learning data file handling now and after following the tutorials here,I think the input part of my program is not working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

#include<iostream>
#include<fstream>
#include<string>
#include <conio.h>

using namespace 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=new char[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 = new char [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.
Topic archived. No new replies allowed.