Previous | Contents | Index |
The available Standard I/O functions position a variable-length or VFC record file at its first byte, at the end-of-file, or on a record boundary. Therefore, the arguments given to fseek must specify any of the following:
See the fgetpos and fsetpos functions for a portable way to seek to arbitrary locations with these types of record files.
If, while accessing a stream file, you seek beyond the end-of-file and then write to the file, the fseek function creates a hole by filling the skipped bytes with zeros. In general, for record files, fseek should only be directed to an absolute position that was returned by a previous valid call to ftell , or to the beginning or end of a file. If a call to fseek does not satisfy these conditions, the results are unpredictable. |
See also open , creat , dup , dup2 , and lseek in this section.
0 Indicates successful seeks. --1 Indicates improper seeks.
Sets the file position indicator for a given file.
#include <stdio.h>int fsetpos (FILE *stream, const 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 .
Call the fgetpos function before using the fsetpos function.
0 Indicates success. --1 Indicates an error.
Accesses information about the file specified by the file descriptor.
#include <stat.h>Function Variants Compiling with the _DECC_V4_SOURCE and _VMS_V6_SOURCE feature-test macros defined enables a local-time-based entry point to this function that is equivalent to the behavior before OpenVMS Version 7.0.int fstat (int file_desc, struct stat *buffer);
file_desc
A file descriptor.buffer
A pointer to a structure of type stat_t , which is defined in the <stat.h> header file. The argument receives information about that particular file. The members of the structure pointed to by buffer are as follows:
Member Type Definition st_dev dev_t Pointer to a physical device name st_ino[3] ino_t Three words to receive the file ID st_mode mode_t File "mode" (prot, dir,...) st_nlink nlink_t For UNIX system compatibility only st_uid uid_t Owner user ID st_gid gid_t Group member: from st_uid st_rdev dev_t UNIX system compatibility -- always 0 st_size off_t File size, in bytes st_atime time_t File access time; always the same as st_mtime st_mtime time_t Last modification time st_ctime time_t File creation time st_fab_rfm char Record format st_fab_rat char Record attributes st_fab_fsz char Fixed header size st_fab_mrs unsigned Record size The types dev_t , ino_t , off_t , mode_t , nlink_t , uid_t , gid_t , and time_t , are defined in the <stat.h> header file. However, when compiling for compatibility (/DEFINE=_DECC_V4_SOURCE), only dev_t , ino_t , and off_t are defined.
As of OpenVMS Version 7.0, times are given in seconds since the Epoch (00:00:00 GMT, January 1, 1970).
The st_mode structure member is the status information mode and is defined in the <stat.h> header file. The st_mode bits follow:
Bits Constant Definition 0170000 S_IFMT Type of file 0040000 S_IFDIR Directory 0020000 S_IFCHR Character special 0060000 S_IFBLK Block special 0100000 S_IFREG Regular 0030000 S_IFMPC Multiplexed char special 0070000 S_IFMPB Multiplexed block special 0004000 S_ISUID Set user ID on execution 0002000 S_ISGID Set group ID on execution 0001000 S_ISVTX Save swapped text even after use 0000400 S_IREAD Read permission, owner 0000200 S_IWRITE Write permission, owner 0000100 S_IEXEC Execute/search permission, owner
This function does not work on remote network files.
0 Indicates successful completion. --1 Indicates an error other than a protection violation. --2 Indicates a protection violation.
Flushes data all the way to the disk.
#include <unistd.h>int fsync (int fd);
fd
A file descriptor corresponding to an open file.
This function behaves much like the fflush function. The primary difference between the two is that fsync flushes data all the way to the disk while fflush flushes data only as far as the underlying RMS buffers. Also, with fflush , you can flush all buffers at once; with fsync you cannot.
0 Indicates successful completion. --1 Indicates an error.
Returns the current byte offset to the specified stream file.
#include <stdio.h>long int ftell (FILE *file_ptr);
file_ptr
A file pointer.
This function measures the byte offset from the beginning of the file.For variable-length files, VFC files, or any file with carriage-control attributes, it the file is opened in record mode, then ftell returns the starting position of the current record, not the current byte offset.
When using record files, the ftell function ignores any characters that have been pushed back using either ungetc or ungetwc . This behavior does not occur if stream files are being used.
For a portable way to measure the exact offset for any type of file, see the fgetpos function.
n The current offset. EOF Indicates an error.
Returns the elapsed time since 00:00:00, January 1, 1970, in the structure pointed at by timeptr.
#include <timeb.h>Function Variants Compiling with the _DECC_V4_SOURCE and _VMS_V6_SOURCE feature-test macros defined enables a local-time-based entry point to this function that is equivalent to the behavior before OpenVMS Version 7.0.int ftime (struct timeb *timeptr);
timeptr
A pointer to the structure timeb_t .
The typedef timeb_t refers to the following structure defined in the <timeb.h> header file:
typedef struct timeb { time_t time; unsigned short millitm; short timezone; short dstflag; };The member time gives the time in seconds; the member millitm gives the fractional time in milliseconds; the members timezone and dstflag (daylight savings time flag) are always 0.
0 Successful execution. The timeb_t structure is filled in. --1 Indicates an error.
Truncates a file to a specified length.
#include <unistd.h>int ftruncate (int filedes, off_t length);
filedes
The descriptor of a file that must be open for writing.length
The new length of the file in bytes.
This function truncates a file at the specified position. For record files, the position must be a record boundary. Also, the files must be local, regular files.If the file was previously larger than length, extra data is lost. If the file was previously shorter than length, bytes between the old and new lengths are read as zeros.
0 Indicates success. --1 An error occurred; errno is set to indicate the error.
Walks a file tree.
#include <ftw.h>int ftw (const char *path, int(*function)(const char *, const struct stat *,int), int depth);
path
The directory hierarchy to be searched.function
The function to be invoked for each file in the directory hierarchy.depth
The maximum number of directory streams or file descriptors, or both, available for use by ftw . This argument should be in the range of 1 to OPEN_MAX.
This function recursively searches the directory hierarchy that descends from the directory specified by the path argument.For each file in the hierarchy, ftw calls the function specified by the function argument, passes it a pointer to a null-terminated character string containing the name of the file, a pointer to a stat structure containing information about the file, and an integer.
The integer identifies the file type. Possible values, defined in <ftw.h> are:
FTW_F Regular file. FTW_D Directory. FTW_DNR Directory that cannot be read. FTW_NS A file on which stat could not successfully be executed. If the integer is FTW_DNR, then the files and subdirectories contained in that directory are not processed.
If the integer is FTW_NS, then the stat structure contents are meaningless. For example, a file in a directory for which you have read permission but not execute (search) permission can cause the function argument to pass FTW_NS.
The ftw function finishes processing a directory before processing any of its files or subdirectories.
The ftw function continues the search until:
- The directory hierarchy specified by the path argument is completed.
- An invocation of the function specified by the function argument returns a nonzero value.
- An error (such as an I/O error) is detected within the ftw function.
Because the ftw function is recursive, it is possible for it to terminate with a memory fault because of stack overflow when applied to very deep file structures.
The ftw function uses the malloc function to allocate dynamic storage during its operation. If ftw is forcibly terminated, as with a call to longjmp from the function pointed to by the function argument, ftw has no chance to free that storage. It remains allocated.
A safe way to handle interrupts is to store the fact that an interrupt has occurred, and arrange to have the function specified by the function argument return a nonzero value the next time it is called.
Note
The ftw function is reentrant; make sure that the function supplied as argument function is also reentrant.See malloc , longjump , lstat , and stat in this section.
0 Indicates success. x Indicates that the function specified by the function argument stops its search, and returns the value that was returned by the function. --1 Indicates an error; errno is set to one of the following values:
- EACCES -- Search permission is denied for any component of the path argument or read permission is denied for the path argument.
- ENAMETOOLONG -- The length of the path string exceeds PATH_MAX, or a pathname component is longer than NAME_MAX while [_POSIX_NO_TRUNC] is in effect.
- ENOENT -- The path argument points to the name of a file that does not exist or points to an empty string.
- ENOMEM -- There is insufficient memory for this operation.
Also, if the function pointed to by the function argument encounters an error, errno can be set accordingly.
Waits for I/O on a specific file to complete.
#include <stdio.h>int fwait (FILE *fp);
fp
A file pointer corresponding to an open file.
This function is used primarily to wait for completion of pending asynchronous I/O.
0 Indicates successful completion. --1 Indicates an error.
Determines and sets the orientation of a stream.
#include <wchar.h>int fwide (FILE *stream, int mode);
stream
A file pointer.mode
A value that specifies the desired orientation of the stream.
This function determines the orientation of the stream pointed to by stream and sets the orientation of a non-oriented stream according to the mode argument in the following way:
If the mode argument is Then the fwide function greater than zero makes the stream wide-oriented. less than zero makes the stream byte-oriented. zero does not alter the orientation of the stream. If the orientation of the stream has already been set, fwide does not alter it. Because no error status is defined for fwide , the calling application should check errno if fwide returns a 0.
> 0 After the call, the stream is wide-oriented. < 0 After the call, the stream is byte-oriented. 0 After the call, the stream has no orientation or a stream argument is invalid; the function sets errno .
Writes a specified number of items to the file.
#include <stdio.h>size_t fwrite (const void *ptr, size_t size_of_item, size_t number_items, FILE *file_ptr);
ptr
A pointer to the memory location from which information is being written. The type of the object pointed to is determined by the type of the item being written.size_of_item
The size, in bytes, of the items being written.number_items
The number of items to be written.file_ptr
A file pointer that indicates the file to which the items are being written.
The type size_t is defined in the header file <stdio.h> as follows:
typedef unsigned int size_tThe writing begins at the current location in the file. The items are written from storage beginning at the location given by the first argument. You must also specify the size of an item, in bytes.
If the file pointed to by file_ptr is a record file, the fwrite function outputs at least number_items records, each of length size_of_item.
x The number of items written. The number of records written depends upon the maximum record size of the file.
Writes output to the stream under control of the wide-character format string.
#include <wchar.h>int fwprintf (FILE *stream, const wchar_t *format, ...);
stream
A file pointer.format
A pointer to a wide-character string containing the format specifications. For more information about format and conversion specifications and their corresponding arguments, 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.
This function writes output to the stream pointed to by stream under control of the wide-character string pointed to by format, which specifies how to convert subsequent arguments to output. If there are insufficient arguments for the format, the behavior is undefined. If the format is exhausted while arguments remain, the excess arguments are evaluated, but are otherwise ignored. The fwprintf function returns when it encounters the end of the format string.The format argument is composed of zero or more directives that include:
- Ordinary wide characters (not the percent sign (%))
- Conversion specifications
n The number of wide characters written. 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 -- Nontranslatable 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 -- Nontranslatable 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