Which of the following opens the file associated with the stream select one?

Predefined Types and Values - FILE, EOF, NULL and size_t

  • FILE is a datatype which holds information about an open file.
  • EOF is a value returned to indicate end-of-file (though is not used exclusively for that purpose) and is required by ANSI C to be a negative integral constant expression and is traditionally set to -1.
  • NULL is set to the value of the null pointer constant 0
  • BUFSIZ is an integer constant which specifies an "appropriate" size for file I/O buffers.
  • size_t is an unsigned integral type which is large enough to hold any value returned by EOF1

Preopened File Streams - EOF2, EOF3, and EOF4

  • EOF5
    • EOF2 is associated with a user's standard input stream.
  • EOF7
    • EOF2 is associated with an output stream used for normal program output.
  • EOF9
    • EOF2 is associated with an output stream used for error messages.

Open File - NULL1 and NULL2

  • NULL3
    • NULL1 opens the file designated by the character string NULL5 and associates it with a stream. The NULL6 string should begin with one of the following sequences:
      • NULL7 - open an existing file for reading, starting at the beginning of the file.
      • NULL8 - truncate an existing file to zero length or create a text file for writing, starting at the beginning of the file.
      • NULL9 - open or create for writing at end of text file.
      • size_t0 - open an existing file for reading and writing, starting at the beginning of the file.
      • size_t1 - truncate an existing file to zero length or create a text file for reading and writing, starting at the beginning of the file.
      • size_t2 - open for reading and writing at end of file or create for reading and writing.
    • The NULL6 string may include a size_t4 as either the second or third character to indicate a binary file.
    • The NULL6 string may also contain other characters after the above modes, which are used in an implementation-defined manner.
    • If a file is opened for update (the size_t6 mode), an output operation may not be followed by an input operation without flushing the buffer (size_t7)) or repositioning (size_t8, size_t9, FILE0), and an input operation may not be followed by an output operation without flushing the buffer or repositioning unless the input operation has reached end-of-file.
    • If NULL1 succeeds, a FILE pointer is returned. Otherwise, NULL is returned and FILE4 is set.
  • FILE5
    • NULL2 behaves exactly like NULL1 except that it associates the newly opened file with FILE8 rather than creating a new stream.
    • NULL2 is primarily used to associate a new file with one of the standard text streams (EOF2, EOF3, or EOF4).

Flush File Buffer - EOF3

  • EOF4
    • EOF3 forces any buffered output to be written, but does not close the stream.
    • If FILE8 is a null pointer, EOF3 flushes all of a process' open output streams (at least on UNIX systems)
    • If the operation succeeds, EOF3 returns 0. Otherwise, EOF is returned and FILE4 is set.

Close File - -11

  • -12
    • -11 causes any buffered output to be written (possibly using EOF3) and then closes the stream.
    • Subsequent attempts to use FILE8 in any routine other than NULL2 will result in errors.
    • If the operation succeeds, -11 returns 0. Otherwise, EOF is returned and FILE4 is set.

Check or Clear File Status - NULL0, NULL1 and NULL2

  • NULL3
    • NULL0 checks the end-of-file indicator for FILE8 and returns non-zero if it is set.
    • Note that even if the last character in a file has been read, an end-of-file condition does not exist until a request is made to read the character after the last character.
  • NULL6
    • NULL1 checks the error indicator for FILE8 and returns non-zero if it is set.
  • NULL9
    • NULL2 clears the end-of-file and error indicators for FILE8.
    • Once an end-of-file or error indicator has been set, it is not reset until NULL2 is called (with the exception that file repositioning functions clear the end-of-file indicator.)

Read Character from File - 03, 04 and 05

  • 06
    • 04 reads the next available character from the input stream FILE8 and returns it as an 09
  • BUFSIZ0
    • 03 is identical in function to 04 but is usually implemented as a macro (which means that FILE8 may be evaluated more than once, so it should not be an expression with side effects.)
  • BUFSIZ4
    • 05 reads the next available character from EOF2 and is typically implemented as BUFSIZ7 (which means it is a macro with all the problems of 03.)
  • Errors and End-Of-File
    • If FILE8 or EOF2 is at end-of-file or a read error occurs, these routines return EOF (and FILE4 is set if an error occurs.) NULL0 or NULL1 must therefore be used to distinguish between the two conditions.

Write Character to File - EOF05, EOF06 and EOF07

  • EOF08
    • EOF06 writes EOF10 to the output stream FILE8 as an EOF12 and returns the character as an 09. If an error occurs, EOF is returned and FILE4 is set.
  • EOF16
    • EOF05 is identical in function to EOF06 but is usually implemented as a macro (which means that FILE8 and EOF10 may be evaluated more than once, so they should not be expressions with side effects.)
  • EOF21
    • EOF07 writes EOF10 to EOF3 and is typically implemented as EOF25 (which means it is a macro with all the problems of EOF05.)

Push Character Back into Buffer - EOF27

  • EOF28
    • EOF27 pushes EOF10 back onto the input stream FILE8, so that it will be returned by a subsequent read of FILE8.
    • Pushed back characters are read in reverse order.
    • If a file repositioning function (size_t8, size_t9, FILE0) is used, any pushed back characters are lost.
    • EOF27 does not affect the contents of the file pointed to by FILE8
    • One character of pushback is guaranteed.
    • Attempts to push EOF have no effect on FILE8 and return EOF

Read String from File - EOF41 and EOF42

  • EOF43
    • EOF41 reads characters from FILE8 and stores them in the string pointed to by EOF46.
    • Reading stops when a newline character is seen, end-of-file is reached or EOF47 characters have been read, and EOF48 is appended to EOF46 (after any newline character.)
    • If end-of-file occurs before any characters have been read, EOF41 returns NULL and the contents of EOF46 are unchanged.
    • If an error occurs at any time during the read operation, EOF41 returns NULL and the contents of EOF46 are undefined.
    • Otherwise, EOF41 returns EOF46
  • EOF58
    • EOF42 is similar to EOF41, but is much more dangerous.
      • EOF42 does not store a newline character.
      • More importantly, EOF42 assumes that EOF46 is infinitely long, allowing sufficiently knowledgeable programmers to worm their way inside the program.

Write String to File - EOF64 and EOF65

  • EOF66
    • EOF64 writes the null-terminated string EOF46 to the output stream FILE8.
    • If an error occurs, EOF64 returns EOF. Otherwise, is returns a nonnegative integer.
  • EOF72
    • EOF65 writes the null-terminated string EOF46, followed by a newline character, to the EOF3 output stream.

Read Binary Data from File - EOF76

  • EOF77
    • EOF76 reads up to EOF79 objects, each EOF80 bytes long, from input stream FILE8, storing them in the memory pointed to by EOF82.
    • The number of objects read is returned.
    • If an error occurs, zero will be returned.
    • If end-of-file is reached, the value returned will be less than EOF79 (and may be zero, in which case NULL0 or NULL1 should be used to distinguish between the two conditions.)

Write Binary Data to File - EOF86

  • EOF87
    • EOF86 writes up to EOF79 objects, each EOF80 bytes long, from the memory pointed to by EOF82 to the output stream FILE8.
    • The number of objects written is returned.
    • If an error occurs, zero will be returned.

Read Formatted Input - EOF93, EOF94, EOF95

  • EOF96
  • EOF97
  • EOF98

Write Formatted Output - EOF99, NULL00, NULL01

  • NULL02
  • NULL03
    • NULL00 writes to output stream FILE8.
  • NULL06
    • NULL01 "writes" its output to the character string NULL08 (followed by a terminating EOF48.)
  • All three functions return the number of characters written (not including the terminating EOF48 for NULL01)

File Position - NULL12, size_t9, FILE0, NULL15, and NULL16

  • NULL17
    • NULL12 stores the value of the current file position indicator for FILE8 in NULL20.
    • NULL20 is an implementation-defined type which may be integral or may be a complex structure.
    • If an error occurs, a non-zero value is returned and FILE4 is set.
  • NULL23
    • size_t9 sets the file position indicator for FILE8 to the position indicated by NULL20.
    • If an error occurs, a non-zero value is returned and FILE4 is set.
    • If size_t9 succeeds, the end-of-file indicator is cleared.
  • NULL29
    • FILE0 sets the file position indicator for FILE8 to the beginning of the file.
  • NULL32
    • NULL15 sets the file position indicator for FILE8. The new byte position is obtained by adding NULL35 to the position specified by NULL36:
      • If NULL36 is set to SEEK_CUR, the offset is computed from the current position in the file.
      • If NULL36 is set to SEEK_SET, the offset is computed from the beginning of the file.
      • If NULL36 is set to SEEK_END, the offset is computed from the end of the file.
        • SEEK_CUR, SEEK_SET, and SEEK_END are all defined in.
    • NULL15 is usually applied to binary files.
  • NULL41
    • NULL16 returns the current file position for FILE8.
    • For binary files, the value returned is the number of bytes from the beginning of the file to the current file position.
    • For text files, the value is implementation-defined, but is guaranteed to be useable in NULL15 and NULL45 must represent the beginning of the file.

Alter File Buffer Size - NULL46 and NULL47

  • NULL48
    • NULL47 sets the type, size and location of the buffer for FILE8.
    • The three types of buffering available are:
      • _IOFBF causes I/O to be block buffered, meaning that bytes are saved up and written when NULL51 has been reached.
      • _IOLBF causes I/O to be line buffered, meaning that the buffer is written when either a newline character is saved to the buffer or when NULL51 has been reached.
      • _IONBF means that no buffering is done; everything is immediately written.
    • If NULL53 is non-null, it is assumed to be at least NULL51 bytes long and will be used instead of the automatically created buffer.
      • The predefined constant BUFSIZ is the recommended value for the buffer size.
      • If NULL53 is NULL, the stream is completely unbuffered.
    • NULL47 can safely be called after a stream has been opened but before any data are read or written.
    • NULL47 returns EOF on error.
  • NULL60
    • NULL46 has the same effect as
      NULL62

Temporary File Functions - NULL63 and NULL64

  • NULL65
    • NULL63 attempts to create a new file and open it using mode NULL67.
    • If the create and open succeed, a FILE pointer is returned.
    • If the file could not be opened, NULL is returned.
    • The file is automatically deleted when it is closed or when the process terminates.
    • Note that this function may create a file which is publicly readable and writable.
  • NULL70
    • NULL64 generates a temporary file name which was not in use when NULL64 was called.
    • If NULL08 is non-null, the file name is copied to that buffer.
      • NULL08 is expected to be at least NULL75 characters long.
    • If NULL08 is null, a static buffer is used, meaning that subsequent calls to NULL64 may overwrite the buffer.
    • Temporary file names use the path prefix NULL78.
    • Both NULL75 and NULL78 are defined in.
    • NULL64 is guaranteed to be able to generate at least TMP_MAX unique temporary file names, where TMP_MAX must be at least 25.
    • Note that there is a race condition between file name selection and file creation.
    • Note also that NULL64 does not create the file and therefore does not ensure the file will be deleted after the program is terminated.
Previous, Next, Index

Which mode of file is used to open a file or stream for insertion?

Here we use out mode that allows us to open the file to write in it. Default Open Modes : ifstream ios::in.

Which of the following file stream class is used for files?

A file stream can be defined using the classes ifstream, ofstream and fstream that contained in the header file fstream. The class to be used depends upon the purpose whether the write data or read data operation is to be performed on the file.

Which method can be used to open a file in file handling?

Opening a file The fopen() function is used to create a file or open an existing file: fp = fopen(const char filename,const char mode); There are many modes for opening a file: r - open a file in read mode.

Which library function associates a stream with the file handle?

The fdopen() function associates an input or output stream with the file that is identified by handle.