Home » C++ Programming » Files and Streams » Question
  1. What is the output of this program?
    #include <iostream>
    #include <fstream>
    using namespace std;
    int main ()
    {
    int len;
    char * buff;
    ifstream is;
    is.open ("Example.txt", ios :: binary );
    is.seekg (0, ios :: end);
    len = is.tellg();
    is.seekg (0, ios :: beg);
    buff = new char [len];
    is.read (buff, len);
    is.close();
    cout.write (buff, len);
    delete[] buff;
    return 0;
    }
    1. Example.text
    2. This is example
    3. Example
    4. Compilation Error
    5. Runtime Error
Correct Option: E

In this program, if the file exist, it will read the file. Otherwise it will throw an exception. A runtime error will occur because the value of the length variable will be “-1” if file doesn’t exist and in line 13 we are trying to allocate an array of size “-1”.



Your comments will be displayed only after manual approval.