Which of the following function is used to open a file in C++?
The keyword parameters can be passed in mixed case. They must be separated by commas. Only one instance of a keyword can be specified. Show The file name passed to fopen() often determines the type of file that is opened. A set of file-naming rules exist, which allow you to create an application that references both MVS™ and HFS files specifically. For details on how fopen() determines the type of file from the filename and mode strings, see the topics about opening files in z/OS XL C/C++ Programming Guide. Large file support for z/OS® UNIX files: Large z/OS UNIX files are supported automatically for AMODE 64 C/C++ applications. AMODE 31 C/C++ applications must be compiled with the option LANGLVL(LONGLONG) and define the _LARGE_FILES feature test macro before any headers are included to enable this function to operate on z/OS UNIX files that are larger than 2 GB in size. File size and offset fields are enlarged to 63 bits in width. Therefore, any other function operating on the file is required to define the _LARGE_FILES feature test macro as well. Named pipes in multithreaded environment: Do not use fopen() to open named pipes in multithreaded environment. If used, a deadlock will occur. See z/OS XL C/C++ Programming Guide for a detailed explanation. File mode Restriction: When running with POSIX(OFF) and specifying a mode parameter that includes 0 or 1, the fopen() request will fail with a message indicating a non-valid mode was specified.Table 1. Values for the Positional ParameterFile ModeGeneral DescriptionrOpen a text file for reading. (The file must exist.)wOpen a text file for writing. If the 2 mode is specified for a ddname that has DISP=MOD, the behavior is the same as if 3 had been specified. Otherwise, if the file already exists, its contents are destroyed.aOpen a text file in append mode for writing at the end of the file. fopen() creates the file if it does not exist.r+Open a text file for both reading and writing. (The file must exist.)w+Open a text file for both reading and writing. If the 4 mode is specified for a ddname that has DISP=MOD, the behavior is the same as if 5 had been specified. Otherwise, if the file already exists, its contents are destroyed.a+Open a text file in append mode for reading or updating at the end of the file. fopen() creates the file if it does not exist.rbOpen a binary file for reading. (The file must exist.)wbOpen an empty binary file for writing. If the 6 mode is specified for a ddname that has DISP=MOD, the behavior is the same as if 7 had been specified. Otherwise, if the file already exists, its contents are destroyed.abOpen a binary file in append mode for writing at the end of the file. fopen() creates the file if it does not exist.rtOpen a text file for reading. (The file must exist.)wtOpen a text file for writing. If the file already exists, its contents are destroyed.atOpen a text file in append mode for writing at the end of the file. fopen() creates the file if it does not exist.r+b or rb+Open a binary file for both reading and writing. (The file must exist.)w+b or wb+Open an empty binary file for both reading and writing. If the 8 (or 9) mode is specified for a ddname that has DISP=MOD, the behavior is the same as if t 0 had been specified. Otherwise, if the file already exists, its contents are destroyed.a+b or ab+Open a binary file in append mode for reading or updating at the end of the file. fopen() creates the file if it does not exist.r+t or rt+Open a text file for both reading and writing. (The file must exist.)w+t or wt+Open a text file for both reading and writing. If the file already exists, its contents are destroyed.a+t or at+Open a text file in append mode for reading or updating at the end of the file. fopen() creates the file if it does not exist.Attention: Use the 2, 4, 6, 8, and 9 parameters with care; data in existing files of the same name will be lost.Text files contain printable characters and control characters organized into lines. Each line ends with a newline character. The system may insert or convert control characters in an output text stream. For example, Note: When compared, data output to a text stream may not be equal to data input from the same text stream. Binary files contain a series of characters. For binary files, the system does not translate control characters on input or output. Under z/OS XL C/C++, some types of files are always treated as binary files, even when opened in text mode. In such cases, a control character is written to the file as binary data. On input, the control character will be read back as it was written. See the topic about the byte stream model in z/OS XL C/C++ Programming Guide for more information. z/OS XL C/C++ has Record I/O and Blocked I/O file extensions. These files are binary in nature—no data interpretation—and require additional qualifiers: When you open a file with 3, 5, 7, rt 3, or t 0 mode, all write operations take place at the end of the file. Although you can reposition the file pointer using fseek(), fsetpos(), or rewind(), the write functions move the file pointer back to the end of the file before they carry out any output operation. This action prevents you from overwriting existing data.When you specify the update mode (using Table 2. Keyword Parameters for File ModeParameterDescription Note: Using wt+ 7Identical to wt+ 5 with the following exceptions:
wt+ 9This parameter specifies that I/O operations against the stream are restricted to the thread in which the stream was opened. The library will not lock the stream in a multithread environment. Use of this keyword can improve performance when the stream does not need to be accessed on different threads.spaceSpace attributes for MVS data sets. Within the parameter, you cannot have any imbedded blanks.Where:uunit type of space requestedpprimary amount of space requestedssecondary amount of space requesteddnumber of directory space requested See the topic about fopen() and freopen() parameters in z/OS XL C/C++ Programming Guide for more information about the syntax of this parameter. t 9This parameter specifies that the file is to be opened for sequential blocked I/O. The file must be opened as a binary file; otherwise, fopen() fails. Read and write operations are done with the fread() and fwrite() functions.w+t 1This parameter identifies this file as a memory file that is accessible only from C programs.w+t 2If you are using MVS/ESA, you can specify the HIPERSPACE suboption to open a hiperspace memory file.Restriction: For AMODE 64 applications, t 8This parameter specifies that the file is to be opened for sequential record I/O. The file must be opened as a binary file; otherwise, fopen() fails. Read and write operations are done with the fread() and fwrite() functions. This is the default fopen() mode for accessing VSAM clusters.Returned valueIf successful, fopen() returns a pointer to the object controlling the associated stream. If unsuccessful, fopen() returns a NULL pointer. fopen() generally fails if parameters are mismatched. Special behavior for large files for HFS: The following is a possible value of errno:Error CodeDescriptionEOVERFLOWThe named file is a regular file and the size of the file cannot be represented correctly in an object of type How to open a file in C?Opening a file is performed using the fopen() function defined in the stdio.h header file. The syntax for opening a file in standard I/O is: ptr = fopen("fileopen","mode");
Which of the following are correct file opening modes in C?Opening File: fopen(). What is the function of files in C?The process of file handling refers to how we store the available data or info in a file with the help of a program. The C language stores all the data available in a program into a file with the help of file handling in C. This data can be fetched/extracted from these files to work again in any program.
|