United States |
Previous | Contents | Index |
Stores the current file position for a given file.
#include <stdio.h>int fgetpos (FILE *stream, fpos_t *pos);
stream
A file pointer.pos
A pointer to an implementation-defined structure. The fgetpos function fills this structure with information that can be used on subsequent calls to fsetpos .
This function stores the current value of the file position indicator for the stream pointed to by stream into the object pointed to by pos.
0 Indicates successful completion. --1 Indicates that there are errors.
#include <stdio.h> #include <stdlib.h> main() { FILE *fp; int stat, i; int character; char ch, c_ptr[130], d_ptr[130]; fpos_t posit; /* Open a file for writing. */ if ((fp = fopen("file.dat", "w+")) == NULL) { perror("open"); exit(1); } /* Get the beginning position in the file. */ if (fgetpos(fp, &posit) != 0) perror("fgetpos"); /* Write some data to the file. */ if (fprintf(fp, "this is a test\n") == 0) { perror("fprintf"); exit(1); } /* Set the file position back to the beginning. */ if (fsetpos(fp, &posit) != 0) perror("fsetpos"); fgets(c_ptr, 130, fp); puts(c_ptr); /* Should be "this is a test." */ /* Close the file. */ if (fclose(fp) != 0) { perror("close"); exit(1); } }
Reads a line from the specified file, up to one less than the specified maximum number of characters or up to and including the new-line character, whichever comes first. The function stores the string in str.
#include <stdio.h>Function Variants This function also has variants named _fgets32 and _fgets64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.8 for more information on using pointer-size-specific functions.char *fgets (char *str, int maxchar, FILE *file_ptr);
str
A pointer to a character string that is large enough to hold the information fetched from the file.maxchar
The maximum number of characters to fetch.file_ptr
A file pointer.
This function terminates the line with a null character (\0). Unlike gets , fgets places the new-line character that terminates the input line into the user buffer if more than maxchar characters have not already been fetched.When the file pointed to by file_ptr is opened in record mode, fgets treats the end of a record the same as a new-line character, so it reads up to and including a new-line character or to the end of the record.
x Pointer to str. NULL Indicates the end-of-file or an error. The contents of str are undefined if a read error occurs.
#include <stdio.h> #include <stdlib.h> #include <unixio.h> main() { FILE *fp; char c_ptr[130]; /* Create a dummy data file */ if ((fp = fopen("file.dat", "w+")) == NULL) { perror("open"); exit(1); } fprintf(fp, "this is a test\n") ; fclose(fp) ; /* Open a file with some data -"this is a test" */ if ((fp = fopen("file.dat", "r+")) == NULL) { perror("open error") ; exit(1); } fgets(c_ptr, 130, fp); puts(c_ptr); /* Display what fgets got. */ fclose(fp); delete("file.dat") ; }
Reads the next character from a specified file, and converts it to a wide-character code.
#include <wchar.h>wint_t fgetwc (FILE *file_ptr);
file_ptr
A pointer to the file to be accessed.
Upon successful completion, the fgetwc function returns the wide-character code read from the file pointed to by file_ptr and converted to type wint_t . If the file is at end-of-file, the end-of-file indicator is set, and WEOF is returned. If an I/O read error occurred, then the error indicator is set, and WEOF is returned.Applications can use ferror or feof to distinguish between an error condition and an end-of-file condition.
x The wide-character code of the character read. WEOF Indicates the end-of-file or an error. If a read error occurs, the function sets errno to one of the following:
- EALREADY -- An operation is already in progress on the same file.
- EBADF -- The file descriptor is not valid.
- EILSEQ -- Invalid character detected.
Reads a line of wide characters from a specified file.
#include <wchar.h>Function Variants This function also has variants named _fgetws32 and _fgetws64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.8 for more information on using pointer-size-specific functions.wchar_t *fgetws (wchar_t *wstr, int maxchar, FILE *file_ptr);
wstr
A pointer to a wide-character string large enough to hold the information fetched from the file.maxchar
The maximum number of wide characters to fetch.file_ptr
A file pointer.
This function reads wide characters from the specified file and stores them in the array pointed to by wstr. The function reads up to maxchar-1 characters or until the newline character is read, converted, and transferred to wstr, or until an end-of-file condition is encountered. The function terminates the line with a null wide character. fgetws places the newline that terminates the input line into the user buffer, unless maxchar characters have already been fetched.
x Pointer to wstr. NULL Indicates the end-of-file or an error occurred. The contents of wstr are undefined if a read error occurs. If a read error occurs, the function sets errno . For a list of possible errno values, see fgetwc in this section.
#include <stdlib.h> #include <stdio.h> #include <locale.h> #include <wchar.h> main() { wchar_t wstr[80], *ret; FILE *fp; /* Create a dummy data file */ if ((fp = fopen("file.dat", "w+")) == NULL) { perror("open"); exit(1); } fprintf(fp, "this is a test\n") ; fclose(fp) ; /* Open a test file containing : "this is a test" */ if ((fp = fopen("file.dat", "r")) == (FILE *) NULL) { perror("File open error"); exit(EXIT_FAILURE); } ret = fgetws(wstr, 80, fp); if (ret == (wchar_t *) NULL) { perror("fgetws failure"); exit(EXIT_FAILURE); } fputws(wstr, stdout); fclose(fp); delete("file.dat"); }
Returns the file descriptor associated with the specified file pointer.
#include <stdio.h>int fileno (FILE *file_ptr);
file_ptr
A file pointer.
If you are using DEC C Version 5.2 or lower, undefine the fileno macro:
#if defined(fileno) #undef fileno #endif
x Integer file descriptor. --1 Indicates an error.
Returns (as a double ) the largest integer that is less than or equal to its argument.
#include <math.h>double floor (double x);
x
A real value.
Computes the floating-point remainder of the first argument divided by the second. If the second argument is 0, the function returns 0.
#include <math.h>double fmod (double x, double y);
x
A real value.y
A real value.
0 Indicates that y is 0. x The value f, which has the same sign as the argument x, such that x == i * y + f for some integer i, where the magnitude of f is less than the magnitude of y.
Opens a file by returning the address of a FILE structure.
#include <stdio.h>FILE *fopen (const char *file_spec, const char *a_mode); (ANSI C)
FILE *fopen (const char *file_spec, const char *a_mode, ...); (DEC C EXTENSION)
file_spec
A character string containing a valid file specification.a_mode
The access mode indicator. Use one of the following character strings: "r", "w", "a", "r+", "w+", "rb", "r+b", "rb+", "wb", "w+b", "wb+", "ab", "a+b", "ab+", or "a+".These access modes have the following effects:
- "r" opens an existing file for reading.
- "w" creates a new file, if necessary, and opens the file for writing. If the file exists, it creates a new file with the same name and a higher version number.
- "a" opens the file for append access. An existing file is positioned at the end-of-file, and data is written there. If the file does not exist, the Compaq C RTL creates it.
The update access modes allow a file to be opened for both reading and writing. When used with existing files, "r+" and "a+" differ only in the initial positioning within the file. The modes are as follows:
- "r+" opens an existing file for read update access. It is opened for reading, positioned first at the beginning-of-file, but writing is also allowed.
- "w+" opens a new file for write update access.
- "a+" opens a file for append update access. The file is first positioned at the end-of-file (writing). If the file does not exist, the Compaq C RTL creates it.
- "b" means binary access mode. In this case, no conversion of carriage-control information is attempted.
...
Optional file attribute arguments. The file attribute arguments are the same as those used in the creat function. For more information, see the creat function.
If a version of the file exists, a new file created with fopen inherits certain attributes from the existing file unless those attributes are specified in the fopen call. The following attributes are inherited:Record format
Maximum record size
Carriage control
File protection.If you specify a directory in the file name and it is a search list that contains an error, Compaq C for OpenVMS Systems interprets it as a file open error.
The file control block can be freed with the fclose function, or by default on normal program termination.
x File pointer. NULL Indicates an error. The constant NULL is defined in the <stdio.h> header file to be the NULL pointer value. The function returns NULL to signal the following errors:
- File protection violations
- Attempts to open a nonexistent file for read access
- Failure to open the specified file
Determine the class of IEEE floating-point values.
#include <math.h>int fp_class (double x);
int fp_classf (float x);
int fp_classl (long double x);
x
An IEEE floating-point number.
These functions determine the class of the specified IEEE floating-point number, returning a constant from the <fp_class.h> header file. They never cause an exception, even for signaling NaNs (Not-a-Number). These functions implement the recommended class(x) function in the appendix of the IEEE 754-1985 standard for binary floating-point arithmetic. The constants in <fp_class.h> refer to the following classes of values:
FP_SNAN Signaling NaN (Not-a-Number) FP_QNAN Quiet NaN FP_POS_INF +infinity FP_NEG_INF --infinity FP_POS_NORM positive normalized FP_NEG_NORM negative normalized FP_POS_DENORM positive denormalized FP_NEG_DENORM negative denormalized FP_POS_ZERO +0.0 (positive zero) FP_NEG_ZERO --0.0 (negative zero)
x A constant from the <fp_class.h> header file.
Retrieves file implementation characteristics.
#include <unistd.h>long int fpathconf (int filedes, int name);
filedes
An open file descriptor.name
The configuration attribute to query. If this attribute is not applicable to the file specified by the filesdes argument, fpathconf returns an error.
This function allows an application to retrieve the characteristics of operations supported by the file system underlying the file named by the filesdes argument. Read, write, or execute permission of the named file is not required, but you must be able to search all directories in the path leading to the file.Symbolic values for the name argument are defined in the <unistd.h> header file as follows:
_PC_LINK_MAX The maximum number of links to the file. If the filedes argument refers to a directory, the value returned applies to the directory itself. _PC_MAX_CANON The maximum number of bytes in a canonical input line. This is applicable only to terminal devices. _PC_MAX_INPUT The number of types allowed in an input queue. This is applicable only to terminal devices. _PC_NAME_MAX Maximum number of bytes in a filename (not including a terminating null). The byte range value is between 13 and 255. This is applicable only to a directory file. The value returned applies to filenames within the directory. _PC_PATH_MAX Maximum number of bytes in a pathname (not including a terminating null). The value is never larger than 65,535. This is applicable only to a directory file. The value returned is the maximum length of a relative pathname when the specified directory is the working directory. _PC_PIPE_BUF Maximum number of bytes guaranteed to be written atomically. This is applicable only to a FIFO. The value returned applies to the referenced object. If the path argument refers to a directory, the value returned applies to any FIFO that exists or can be created within the directory. _PC_CHOWN_RESTRICTED The value returned applies to any files (other than directories) that exist or can be created within the directory. This is applicable only to a directory file. _PC_NO_TRUNC Returns 1 if supplying a component name longer than allowed by NAME_MAX causes an error. Returns 0 (zero) if long component names are truncated. This is applicable only to a directory file. _PC_VDISABLE This is always 0 (zero); no disabling character is defined. This is applicable only to a terminal device.
x The resultant value for the configuration attribute specified in the name argument. --1 Indicates an error; errno is set to one of the following values:
- EINVAL -- The name argument specifies an unknown or inapplicable characteristic.
- EBADF -- the filedes argument is not a valid file descriptor.
Performs formatted output to a specified file.
#include <stdio.h>int fprintf (FILE *file_ptr, const char *format_spec, ...);
file_ptr
A pointer to the file to which the output is directed.format_spec
A pointer to a character string that contains the format specification. For more information on format specifications and conversion characters, see Chapter 2....
Optional expressions whose resultant types correspond to conversion specifications given in the format specification.If no conversion specifications are given, the output sources can be omitted. Otherwise, the function calls must have exactly as many output sources as there are conversion specifications, and the conversion specifications must match the types of the output sources.
Conversion specifications are matched to output sources in left-to-right order. Any excess output sources are ignored.
An example of a conversion specification follows:
#include <stdio.h> main() { int temp = 4, temp2 = 17; fprintf(stdout, "The answers are %d, and %d.", temp, temp2); }Sample output (to the stdout file) from the previous example is as follows:
The answers are 4, and 17.For a complete description of the format specification and the output source, see Chapter 2.
x The number of bytes written, excluding the null terminator. Negative value Indicates an error. The function sets errno to one of the following:
- EILSEQ -- Invalid character detected.
- EINVAL -- Insufficient arguments.
- ENOMEM -- Not enough memory available for conversion.
- ERANGE -- Floating-point calculations overflow.
- EVMSERR -- Non-translatable VMS error. vaxc$errno contains the VMS error code. This might indicate that conversion to a numeric value failed because of overflow.
The function can also set errno to the following as a result of errors returned from the I/O subsystem:
- EBADF -- The file descriptor is not valid.
- EIO -- I/O error.
- ENOSPC -- No free space on the device containing the file.
- ENXIO -- Device does not exist.
- EPIPE -- Broken pipe.
- ESPIPE -- Illegal seek in a file opened for append.
- EVMSERR -- Non-translatable VMS error. vaxc$errno contains the VMS error code. This indicates that an I/O error occurred for which there is no equivalent C error code.
Previous | Next | Contents | Index |
|