Files and Streams


  1. What is the output of this program?
    #include <iostream>
    int main ()
    {
    FILE * ptr;
    long size;
    ptr = fopen ("Example.txt", "rb");
    if (ptr == NULL)
    perror ("Error opening file");
    else
    {
    fseek (ptr, 0, SEEK_END);
    size = ftell (ptr);
    fclose (ptr);
    printf (" %ld\n", size);
    }
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    This program will return the size of the file in bytes.


  1. What is the output of this program in the file?
    #include <iostream>
    int main ()
    {
    freopen ("Sample.txt", "w", stdout);
    printf ("This is redirected to a file");
    fclose (stdout);
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    In this program, We are sending the text to the file by opening the file using the function freopen.



  1. What is the output of this program?
    #include <iostream>
    int main ()
    {
    int k;
    FILE * ptr;
    char buff[10];
    ptr = fopen ("Sample.txt", "w+");
    for ( k = 'A' ; k <= 'D' ; k++)
    fputc ( k, ptr);
    rewind (ptr);
    fread (buff, 1, 5, ptr);
    fclose (ptr);
    buff[3] = '\0';
    puts (buff);
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    In this program, We are setting the buffer size upto 3 only, So it is printing ABC.


  1. What is the output of this program in the text file?
    #include 
    int main ()
    {
    FILE * ptr;
    char buff[] = { 'I' , 'L' , 'U' };
    ptr = fopen ( "Example.txt" , "wb" );
    fwrite (buff , 1 , sizeof(buff) , ptr );
    fclose (ptr);
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    In this program, We are writing into the file by using the function fwrite.



  1. By using which function does the buffer are automatically flushed?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    fclose