13.16 <cstdio>The <cstdio> header is a wrapper for the C standard <stdio.h> header, which declares input and output types, macros, and functions. See also <cwchar> for wide character I/O functions. C++ I/O streams offer more flexibility, type-safety, and clarity. On the other hand, C I/O offers simplicity and compatibility with C libraries. See Chapter 9 for an overview of the C++ I/O stream classes.
When passed as the mode parameter to setvbuf, the _IOFBF macro sets an open file to full buffering. A buffer is flushed when it is full. The _IOFBF macro expands to a constant integer. Support for fully-buffered streams is implementation-dependent. See Alsosetvbuf function
When passed as the mode parameter to setvbuf, the _IOLBF macro sets an open file to line buffering. A buffer is flushed when it is full or when a newline character is read or written. The _IOLBF macro expands to a constant integer. Support for line-buffered streams is implementation-dependent. See Alsosetvbuf function
When passed as the mode parameter to setvbuf, the _IONBF macro disables the buffering of an open file. Characters are read or written as soon as possible, without buffering. The _IONBF macro expands to a constant integer. Support for unbuffered streams is implementation-dependent. For example, a host operating system might line buffer input from a terminal, even if a program requests unbuffered input. See Alsosetvbuf function
The BUFSIZ macro specifies the minimum buffer size for the setbuf function. The BUFSIZ macro expands to a constant integer. See Alsosetbuf function, setvbuf function
The clearerr function clears the error and end-of-file indicators for stream. See Alsofeof function, ferror function
The EOF macro represents end-of-file when returned from getchar and other functions. Some functions return EOF to indicate an error. The value of EOF is a negative integer constant. The precise value is implementation-defined.
The fclose function flushes and closes an open file. It returns 0 upon success or EOF when there is an error. See Alsofopen function
The feof function returns true (nonzero) if stream is positioned at the end-of-file, or false (0) otherwise. See Alsoclearerr function, ferror function
The ferror function returns true (nonzero) if stream has an error condition set, or false (0) otherwise. See Alsoclearerr function, feof function
The fgetc function reads a single character from stream. It returns the character as an unsigned char converted to int or EOF for an error or end-of-file. See Alsofeof function, ferror function, getc macro, fputc function, fwgetc in <cwchar>
The fgetpos function stores stream's current position in the object that pos points to. The only use for the position is to save it and pass it to fsetpos to set the file's position. You cannot use the position arithmetically, e.g., to advance the position by one character. The return value is 0 for success or nonzero for failure. If fgetpos fails, it sets errno. See Alsofpos_t type, fsetpos function, ftell function
The fgets function reads a line of text from stream into the character array that s points to. It stops reading after a newline character or after n - 1 characters have been read. The newline character, if one is encountered, is copied into s. The return value is s for success or a null pointer for an error or end-of-file. If fgets fails, the contents of the string s are undefined. See Alsofgetc function, getc macro, fputs function, fwgets in <cwchar>
The FILE type represents the contents of an external file. A C++ program works with FILE pointers, in which the actual FILE objects are managed by functions in the standard library. Thus, you never need to allocate or free FILE objects. See Alsofclose function, fopen function, freopen function, <fstream>
FILENAME_MAX is the size you should use when declaring a character array that will store a filename. Some systems do not have a fixed maximum size for a filename, in which case FILENAME_MAX is a recommended size, and the resulting character array might not be large enough to hold all valid filenames. The FILENAME_MAX macro expands to a constant integer.
The fopen function opens a file. The filename parameter specifies the filename in an implementation-defined manner. The mode parameter specifies how to open the file. The mode must begin with one of the strings listed in Table 13-4. Additional characters can follow, and the interpretation of the extra characters is implementation-defined. Mode strings are case-sensitive.
See Alsofclose function, freopen function
A typical operating system has a maximum number of files that can be open at one time. This number might be variable or fixed; FOPEN_MAX is the guaranteed minimum value of the limit. The FOPEN_MAX macro expands to a constant integer. See Alsofopen function
The fpos_t type is an opaque type that represents a position in a file. The only way to set the value of an fpos_t object is to call fgetpos, and the only things you can do with the value are assign an fpos_t value to it and pass it as a function argument, especially to fsetpos. See Alsofgetpos function, fsetpos function, fpos in <ios>
The fprintf function writes formatted output to stream. The format parameter contains the formatting information, and the remaining arguments are printed according to the format. The return value is the number of characters printed, or a negative value for an error. Characters in format are printed verbatim except for conversion specifications, which begin with a percent sign (%). Each conversion specification is made up of the following parts (in order): flags, field width, precision, size, and conversion specifier. The following are detailed descriptions of the parts of a conversion specification:
All the printf-related functions interpret the format string identically. ExampleThe following are examples of calling printf: long double pi = 3.141592653589792L; int i = 42; const char greeting[] = "Hello, how are you?"; printf(">%d %% %Lg<\n", i, pi); // Prints >42 % 3.14159< printf(">%4d<\n", i); // Prints > 42< printf(">%-16.8Le<\n", pi); // Prints >3.14159265e+00 < printf(">%#*.*x<\n", 8, 4, i); // Prints > 0x002a< printf(">%.5s<\n", greeting); // Prints >Hello< See Alsofscanf function, printf function, sprintf function, vfprintf function, wcrtomb in <cwchar>, fwprintf in <cwchar>
The fputc function writes a single character to stream. The character must be an unsigned char, which is automatically promoted to int, so the proper way to print a variable of type char is as follows: char ch; fputc(static_cast<unsigned char>(ch), stream); The return value is EOF for an error or c for success. See Alsoputc macro, fwputc in <cwchar>
The fputs function writes the string s to stream. It returns EOF for an error or a nonnegative value for success. See Alsofputc function, puts function, fwputs in <cwchar>
The fread function reads up to count elements from stream into the memory that ptr points to. The memory must have POD type (see Chapter 6). Each element is size bytes long. It returns the number of elements that were read successfully. See Alsofwrite function
The freopen function opens a file using an existing stream. The file previously associated with the stream is closed first, and the named file is opened in the same manner as if fopen were called. See fopen for a description of the mode parameter. The main purpose of using freopen is to reopen one of the standard files: stdin, stdout, and stderr. See Alsofclose function, fopen function
The fscanf function performs a formatted read from stream. The format parameter contains formatting information, and the remaining arguments are pointers. When fscanf reads items, it stores their values in the objects that successive arguments point to. The return value is the number of items read or a negative value for an error. Items are read from stream and interpreted according to format, which contains whitespace characters, non-whitespace characters, and conversion specifications, which begin with a percent sign (%). A whitespace character directs fscanf to skip over whitespace in the input stream. Non-whitespace characters must match the input text. Each conversion specification is made up of the following parts (in order): assignment suppression, field width, size, and conversion specifier. The following are descriptions of the conversion specification elements:
All the scanf-related functions interpret the format string identically. ExampleThe following is an example of calling scanf. The input stream is: start 3.14 BeefFeed cab42.0e-01, 1234 and the scanf call is: char c[10]; double d; float f; long int l; unsigned short us; scanf("start %4lf %4lx%*x %9[abcdefg]%f,%hu", &d, &l, c, &f, &us); which has the following result: c = "cab" d = 3.14 f = 4.2 l = 48879 (0xbeef) us = 1234 See Alsofprintf function, scanf function, sscanf function, vfscanf function, mbrtowc in <cwchar>, fwscanf in <cwchar>
The fseek function seeks to a different position in stream. The origin must be one of SEEK_CUR, SEEK_END, or SEEK_SET. The offset is relative to the current position, end-of-file, or start-of-file, respectively. The end-of-file flag is cleared, and any ungetc character is also cleared. Use fsetpos instead of fseek when using large files—that is, for which a position does not necessarily fit into a long int. The return value is 0 for success or nonzero for an error. See Alsofsetpos function, ftell function, SEEK_CUR macro, SEEK_END macro, SEEK_SET macro
The fsetpos function seeks to a different position in stream. The position must have been returned from an earlier successful call to fgetpos. See Alsofpos_t type, fseek function, fgetpos function
The ftell function returns the current file position in stream. This position can be used (with an origin of SEEK_SET) in a subsequent call to fseek. If ftell fails, it returns -1L (which may be, but is not necessarily, the same value as EOF). See Alsofgetpos function, fseek function
The fwrite function writes up to count elements to stream. Each element is size bytes long, and ptr points to the first such element. The memory must have POD type (see Chapter 6). The return value is the number of complete elements successfully written to stream. See Alsofread function
The getc macro reads a character from stream and returns that character as an unsigned char. The return value is EOF for end-of-file or a read error. See Alsofgetc function, getchar macro, putc macro
The getchar macro is equivalent to getc(stdin). See Alsofgetc function, getc macro, putchar macro
The gets function reads a line of text (up to and including a newline) into the string s.
See Alsofgets function, getchar macro
L_tmpnam is the length of a temporary filename for tmpnam. The L_tmpnam macro expands to a constant integer. See Alsotmpnam function
The NULL macro expands to a null pointer constant. See <cstddef> for more information. See AlsoNULL in <cstddef>
The perror function writes an error message to stderr. If s is not null and is not an empty string, it is printed first, followed by a colon and a space. The error message is printed next and is the same text as that returned by strerror, passing errno as its argument. See Alsoerrno in <cerrno>, strerror in <cstring>
The printf function is equivalent to calling fprintf to stdout. See fprintf for information about the format string. See Alsofprintf function, vprintf function, wprintf in <cwchar>
The putc macro writes the character c, which must be cast to unsigned char, to stream. See Alsofputc function, putchar macro, wputc in <cwchar>
The putchar macro is equivalent to putc(c, stdout). See Alsofputc function, putc macro, wputchar in <cwchar>
The puts function writes a string to stdout. See Alsofputs function, putc function, wputs in <cwchar>
The remove function deletes the file named by filename. It returns 0 for success or nonzero for an error. See Alsorename function
The rename function renames the file specified by oldname to newname. The return value is 0 for success or nonzero for failure. See Alsoremove function
The rewind function moves a file position to the beginning of a file and is equivalent to fseek(stream, 0, SEEK_SET). See Alsofseek function, fsetpos function
Pass SEEK_CUR as the last parameter to fseek to seek from the current file position. Positive offset values seek toward the end of the file, and negative values seek toward the beginning of the file. The SEEK_CUR macro expands to a constant integer. See Alsofseek function
Pass SEEK_END as the last parameter to fseek to seek relative to the end of the file. Positive offset values seek past the end of the file, and negative values seek toward the beginning of the file. The SEEK_END macro expands to a constant integer. See Alsofseek function
Pass SEEK_SET as the last parameter to fseek to seek from the start of the file. Positive offset values seek toward the end of the file. The SEEK_SET macro expands to a constant integer. See Alsofseek function
The setbuf function sets the buffer to use when reading from or writing to stream. The size of buf must be at least BUFSIZ characters. See AlsoBUFSIZ macro, setvbuf function
The setvbuf function sets the buffering for stream. The mode determines the buffering mode: no buffering (_IONBF), line buffering (_IOLBF), or full buffering (_IOFBF). You can supply a buffer in the buf argument, with size as the buffer size, or use a null pointer for the buf argument to let setvbuf allocate the buffer. (The buffer will be freed when the file is closed or setvbuf is called to change the buffering.) Call setvbuf before performing any I/O on stream. See AlsoIOFBF macro, _IOLBF macro, _IONBF macro, setbuf function
The size_t type is the type of the result of the sizeof operator. It is an unsigned integral type. The exact type is implementation-defined. See Alsosize_t in <cstddef>
The sprintf function is like fprintf, but instead of writing to an open file, it "writes" by copying characters to the string s. See fprintf for a description of the format parameter. You must ensure that s is large enough for the formatted string. or some formats, this is impossible, which makes sprintf unsafe to use. See Alsofprintf function, sscanf function, vsprintf function, wsprintf in <cwchar>, <sstream>
The sscanf function is like fscanf, but instead of reading from an open file, it "reads" characters from the string s. See fscanf for a description of the format parameter. See Alsofscanf function, sprintf function, vsscanf function, swscanf in <cwchar>, <sstream>
The stderr macro is a standard file, suitable for printing error messages. Its buffering is implementation-defined: either unbuffered or line buffered. See Alsocerr in <iostream>, clog in <iostream>
The stdin macro is a standard file used for reading from the program's standard input device. stdin is fully buffered if the standard input is not an interactive device. See Alsocin in <iostream>
The stdout macro is a standard file used to print to the program's standard output device. stdout is fully buffered if the standard output is not an interactive device. See Alsocout in <iostream>
The TMP_MAX macro is the number of unique names the tmpnam function generates. See Alsotmpnam function
The tmpfile function generates a unique filename and opens a file with that name using mode "wb+". When the program terminates normally, the file is automatically deleted. If the temporary file cannot be created, a null pointer is returned. See Alsotmpnam function
The tmpnam function generates a unique filename and returns a pointer to that name. If the s parameter is not null, it must point to an array of at least L_tmpnam characters, the new filename is copied into that array, and s is returned. If s is null, a static character array is returned; the contents of the array are overwritten each time tmpnam is called.
See AlsoL_tmpnam macro, tmpfile function
The ungetc function pushes back the character c (which must be an unsigned char), so the next read from stream will return c. The standard guarantees that you can push back just one character, though in some situations, you may be able to push back more. The return value is c for success or EOF for an error. See Alsofgetc function, getc macro, getchar macro, ungetwc in <cwchar>
The vfprintf function is like fprintf, but the values to print are taken from successive arguments in arg (obtained by calling va_start(arg, param)). Use vfprintf to write your own fprintf-like function. ExampleThe following shows how you can implement fprintf in terms of vfprintf: int fprintf(std::FILE* stream, const char* format, . . . ) { std::va_list ap; va_start(ap, format); int result = vfprintf(stream, format, ap); va_end(ap); return result; } See Alsofprintf function, vfwprintf in <cwchar>, <cstdarg>
The vprintf function is like printf, but the values to print are taken from successive arguments in arg (obtained by calling va_start(arg, param)). Use vprintf to write your own printf-like function. See Alsoprintf function, vwprintf in <cwchar>, <cstdarg>
The vsprintf function is like sprintf, but the values to print are taken from successive arguments in arg (obtained by calling va_start(arg, param)). Use vsprintf to write your own sprintf-like function. See Alsosprintf function, vswprintf in <cwchar>, <cstdarg> |