Exercise: Input Output

Questions for: Input Output

We should not read after a write to a file without an intervening call to fflush(), fseek() or rewind()
A:
True
B:
False
C:
D:
Answer: A

True, we should not be able to read a file after writing in that file without calling the below functions.

int fflush ( FILE * stream ); If the given stream was open for writing and the last i/o operation was an output operation, any unwritten data in the output buffer is written to the file.

int fseek ( FILE * stream, long int offset, int origin ); Its purpose is to change the file position indicator for the specified stream.

void rewind ( FILE * stream ); Sets the position indicator associated with stream to the beginning of the file.

Offset used in fseek() function call can be a negative number.
A:
True
B:
False
C:
D:
Answer: A

True, offset in fseek() function can be a negative number. It makes the file pointer to move backwards from the current position.

Declaration: retval = fseek( fp, offset, from );

Where:

FILE *fp; = points to the file on which I/O is to be repositioned.

long offset; = is an integer giving the number of bytes to move forward or backward in the file. This may be positive or negative.

int from; = is one of the manifests SEEK_SET, SEEK_CUR, or SEEK_END.

int retval; = is non-zero if the seek operation was invalid (e.g. on a file not opened with a "b" option); otherwise, the return value is zero.

A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character.
A:
True
B:
False
C:
D:
Answer: A

True, each line may contain zero or more characters terminated by a newline character.

While calling the fprintf() function in the format string conversion specifier %s can be used to write a character string in capital letters.
A:
True
B:
False
C:
D:
Answer: B

The %s format specifier tells the compiler the given input was string of characters.

Point out the correct statements about the program?
#include<stdio.h>

int main()
{
    FILE *fptr;
    char str[80];
    fptr = fopen("f1.dat", "w");
    if(fptr == NULL)
        printf("Cannot open file");
    else
    {
        while(strlen(gets(str))>0)
        {
            fputs(str, fptr);
            fputs("\n", fptr);
        }
        fclose(fptr);
    }
    return 0;
}
A:
The code copies the content of one file to another
B:
The code writes strings that are read from the keyboard into a file.
C:
The code reads a file
D:
None of above
Answer: B

This program get the input string from the user through gets function and store it in the file f1.txt using fputs function.

Ad Slot (Above Pagination)
Quiz