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 constant0
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 byEOF
1
Preopened File Streams - EOF
2, EOF
3, and EOF
4
EOF
5EOF
2 is associated with a user's standard input stream.
EOF
7EOF
2 is associated with an output stream used for normal program output.
EOF
9EOF
2 is associated with an output stream used for error messages.
Open File - NULL
1 and NULL
2
NULL
3NULL
1 opens the file designated by the character stringNULL
5 and associates it with a stream. TheNULL
6 string should begin with one of the following sequences:NULL
7 - open an existing file for reading, starting at the beginning of the file.NULL
8 - truncate an existing file to zero length or create a text file for writing, starting at the beginning of the file.NULL
9 - open or create for writing at end of text file.size_t
0 - open an existing file for reading and writing, starting at the beginning of the file.size_t
1 - truncate an existing file to zero length or create a text file for reading and writing, starting at the beginning of the file.size_t
2 - open for reading and writing at end of file or create for reading and writing.
- The
NULL
6 string may include asize_t
4 as either the second or third character to indicate a binary file. - The
NULL
6 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_t
6 mode], an output operation may not be followed by an input operation without flushing the buffer [size_t
7]] or repositioning [size_t
8,size_t
9,FILE
0], 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
NULL
1 succeeds, aFILE
pointer is returned. Otherwise,NULL
is returned andFILE
4 is set.
FILE
5NULL
2 behaves exactly likeNULL
1 except that it associates the newly opened file withFILE
8 rather than creating a new stream.NULL
2 is primarily used to associate a new file with one of the standard text streams [EOF
2,EOF
3, orEOF
4].
Flush File Buffer - EOF
3
EOF
4EOF
3 forces any buffered output to be written, but does not close the stream.- If
FILE
8 is a null pointer,EOF
3 flushes all of a process' open output streams [at least on UNIX systems] - If the operation succeeds,
EOF
3 returns 0. Otherwise,EOF
is returned andFILE
4 is set.
Close File - -1
1
-1
2-1
1 causes any buffered output to be written [possibly usingEOF
3] and then closes the stream.- Subsequent attempts to use
FILE
8 in any routine other thanNULL
2 will result in errors. - If the operation succeeds,
-1
1 returns 0. Otherwise,EOF
is returned andFILE
4 is set.
Check or Clear File Status - NULL
0, NULL
1 and NULL
2
NULL
3NULL
0 checks the end-of-file indicator forFILE
8 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.
NULL
6NULL
1 checks the error indicator forFILE
8 and returns non-zero if it is set.
NULL
9NULL
2 clears the end-of-file and error indicators forFILE
8.- Once an end-of-file or error indicator has been set, it is not reset until
NULL
2 is called [with the exception that file repositioning functions clear the end-of-file indicator.]
Read Character from File - 0
3, 0
4 and 0
5
0
60
4 reads the next available character from the input streamFILE
8 and returns it as an0
9
BUFSIZ
00
3 is identical in function to0
4 but is usually implemented as a macro [which means thatFILE
8 may be evaluated more than once, so it should not be an expression with side effects.]
BUFSIZ
40
5 reads the next available character fromEOF
2 and is typically implemented asBUFSIZ
7 [which means it is a macro with all the problems of0
3.]
- Errors and End-Of-File
- If
FILE
8 orEOF
2 is at end-of-file or a read error occurs, these routines returnEOF
[andFILE
4 is set if an error occurs.]NULL
0 orNULL
1 must therefore be used to distinguish between the two conditions.
- If
Write Character to File - EOF
05, EOF
06 and EOF
07
EOF
08EOF
06 writesEOF
10 to the output streamFILE
8 as anEOF
12 and returns the character as an0
9. If an error occurs,EOF
is returned andFILE
4 is set.
EOF
16EOF
05 is identical in function toEOF
06 but is usually implemented as a macro [which means thatFILE
8 andEOF
10 may be evaluated more than once, so they should not be expressions with side effects.]
EOF
21EOF
07 writesEOF
10 toEOF
3 and is typically implemented asEOF
25 [which means it is a macro with all the problems ofEOF
05.]
Push Character Back into Buffer - EOF
27
EOF
28EOF
27 pushesEOF
10 back onto the input streamFILE
8, so that it will be returned by a subsequent read ofFILE
8.- Pushed back characters are read in reverse order.
- If a file repositioning function [
size_t
8,size_t
9,FILE
0] is used, any pushed back characters are lost. EOF
27 does not affect the contents of the file pointed to byFILE
8- One character of pushback is guaranteed.
- Attempts to push
EOF
have no effect onFILE
8 and returnEOF
Read String from File - EOF
41 and EOF
42
EOF
43EOF
41 reads characters fromFILE
8 and stores them in the string pointed to byEOF
46.- Reading stops when a newline character is seen, end-of-file is reached or
EOF
47 characters have been read, andEOF
48 is appended toEOF
46 [after any newline character.] - If end-of-file occurs before any characters have been read,
EOF
41 returnsNULL
and the contents ofEOF
46 are unchanged. - If an error occurs at any time during the read operation,
EOF
41 returnsNULL
and the contents ofEOF
46 are undefined. - Otherwise,
EOF
41 returnsEOF
46
EOF
58EOF
42 is similar toEOF
41, but is much more dangerous.EOF
42 does not store a newline character.- More importantly,
EOF
42 assumes thatEOF
46 is infinitely long, allowing sufficiently knowledgeable programmers to worm their way inside the program.
Write String to File - EOF
64 and EOF
65
EOF
66EOF
64 writes the null-terminated stringEOF
46 to the output streamFILE
8.- If an error occurs,
EOF
64 returnsEOF
. Otherwise, is returns a nonnegative integer.
EOF
72EOF
65 writes the null-terminated stringEOF
46, followed by a newline character, to theEOF
3 output stream.
Read Binary Data from File - EOF
76
EOF
77EOF
76 reads up toEOF
79 objects, eachEOF
80 bytes long, from input streamFILE
8, storing them in the memory pointed to byEOF
82.- 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
EOF
79 [and may be zero, in which caseNULL
0 orNULL
1 should be used to distinguish between the two conditions.]
Write Binary Data to File - EOF
86
EOF
87EOF
86 writes up toEOF
79 objects, eachEOF
80 bytes long, from the memory pointed to byEOF
82 to the output streamFILE
8.- The number of objects written is returned.
- If an error occurs, zero will be returned.
Read Formatted Input - EOF
93, EOF
94, EOF
95
EOF
96EOF
97EOF
98
Write Formatted Output - EOF
99, NULL
00, NULL
01
NULL
02NULL
03NULL
00 writes to output streamFILE
8.
NULL
06NULL
01 "writes" its output to the character stringNULL
08 [followed by a terminatingEOF
48.]
- All three functions return the number of characters written [not including the terminating
EOF
48 forNULL
01]
File Position - NULL
12, size_t
9, FILE
0, NULL
15, and NULL
16
NULL
17NULL
12 stores the value of the current file position indicator forFILE
8 inNULL
20.NULL
20 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
FILE
4 is set.
NULL
23size_t
9 sets the file position indicator forFILE
8 to the position indicated byNULL
20.- If an error occurs, a non-zero value is returned and
FILE
4 is set. - If
size_t
9 succeeds, the end-of-file indicator is cleared.
NULL
29FILE
0 sets the file position indicator forFILE
8 to the beginning of the file.
NULL
32NULL
15 sets the file position indicator forFILE
8. The new byte position is obtained by addingNULL
35 to the position specified byNULL
36:- If
NULL
36 is set to SEEK_CUR, the offset is computed from the current position in the file. - If
NULL
36 is set to SEEK_SET, the offset is computed from the beginning of the file. - If
NULL
36 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.
- If
NULL
15 is usually applied to binary files.
NULL
41NULL
16 returns the current file position forFILE
8.- 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
NULL
15 andNULL
45 must represent the beginning of the file.
Alter File Buffer Size - NULL
46 and NULL
47
NULL
48NULL
47 sets the type, size and location of the buffer forFILE
8.- The three types of buffering available are:
- _IOFBF causes I/O to be block buffered, meaning that bytes are saved up and written when
NULL
51 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
NULL
51 has been reached. - _IONBF means that no buffering is done; everything is immediately written.
- _IOFBF causes I/O to be block buffered, meaning that bytes are saved up and written when
- If
NULL
53 is non-null, it is assumed to be at leastNULL
51 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
NULL
53 isNULL
, the stream is completely unbuffered.
NULL
47 can safely be called after a stream has been opened but before any data are read or written.NULL
47 returnsEOF
on error.
NULL
60NULL
46 has the same effect asNULL
62
Temporary File Functions - NULL
63 and NULL
64
NULL
65NULL
63 attempts to create a new file and open it using modeNULL
67.- 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.
NULL
70NULL
64 generates a temporary file name which was not in use whenNULL
64 was called.- If
NULL
08 is non-null, the file name is copied to that buffer.NULL
08 is expected to be at leastNULL
75 characters long.
- If
NULL
08 is null, a static buffer is used, meaning that subsequent calls toNULL
64 may overwrite the buffer. - Temporary file names use the path prefix
NULL
78. - Both
NULL
75 andNULL
78 are defined in. NULL
64 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
NULL
64 does not create the file and therefore does not ensure the file will be deleted after the program is terminated.
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.