⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 llio.texi

📁 一个C源代码分析器
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
macro @code{TEMP_FAILURE_RETRY}, as follows:@smallexamplenbytes = TEMP_FAILURE_RETRY (write (desc, buffer, count));@end smallexampleThe @code{write} function is the underlying primitive for all of thefunctions that write to streams, such as @code{fputc}.@end deftypefun@node File Position Primitive@section Setting the File Position of a DescriptorJust as you can set the file position of a stream with @code{fseek}, youcan set the file position of a descriptor with @code{lseek}.  Thisspecifies the position in the file for the next @code{read} or@code{write} operation.  @xref{File Positioning}, for more informationon the file position and what it means.To read the current file position value from a descriptor, use@code{lseek (@var{desc}, 0, SEEK_CUR)}.@cindex file positioning on a file descriptor@cindex positioning a file descriptor@cindex seeking on a file descriptor@comment unistd.h@comment POSIX.1@deftypefun off_t lseek (int @var{filedes}, off_t @var{offset}, int @var{whence})The @code{lseek} function is used to change the file position of thefile with descriptor @var{filedes}.The @var{whence} argument specifies how the @var{offset} should beinterpreted in the same way as for the @code{fseek} function, and can beone of the symbolic constants @code{SEEK_SET}, @code{SEEK_CUR}, or@code{SEEK_END}.@table @code@item SEEK_SETSpecifies that @var{whence} is a count of characters from the beginningof the file.@item SEEK_CURSpecifies that @var{whence} is a count of characters from the currentfile position.  This count may be positive or negative.@item SEEK_ENDSpecifies that @var{whence} is a count of characters from the end ofthe file.  A negative count specifies a position within the currentextent of the file; a positive count specifies a position past thecurrent end.  If you set the position past the current end, and actually write data, you will extend the file with zeros up to thatposition.@end tableThe return value from @code{lseek} is normally the resulting fileposition, measured in bytes from the beginning of the file.You can use this feature together with @code{SEEK_CUR} to read thecurrent file position.You can set the file position past the current end of the file.  Thisdoes not by itself make the file longer; @code{lseek} never changes thefile.  But subsequent output at that position will extend the file.Characters between the previous end of file and the new positionare filled with zeros.If the file position cannot be changed, or the operation is in some wayinvalid, @code{lseek} returns a value of @code{-1}.  The following@code{errno} error conditions are defined for this function:@table @code@item EBADFThe @var{filedes} is not a valid file descriptor.@item EINVALThe @var{whence} argument value is not valid, or the resultingfile offset is not valid.@item ESPIPEThe @var{filedes} corresponds to a pipe or FIFO, which cannot be positioned.(There may be other kinds of files that cannot be positioned either, butthe behavior is not specified in those cases.)@end tableThe @code{lseek} function is the underlying primitive for the@code{fseek}, @code{ftell} and @code{rewind} functions, which operate onstreams instead of file descriptors.@end deftypefunYou can have multiple descriptors for the same file if you open the filemore than once, or if you duplicate a descriptor with @code{dup}.  Descriptors that come from separate calls to @code{open} have independentfile positions; using @code{lseek} on one descriptor has no effect on theother.  For example, @smallexample@group@{  int d1, d2;  char buf[4];  d1 = open ("foo", O_RDONLY);  d2 = open ("foo", O_RDONLY);  lseek (d1, 1024, SEEK_SET);  read (d2, buf, 4);@}@end group@end smallexample@noindentwill read the first four characters of the file @file{foo}.  (Theerror-checking code necessary for a real program has been omitted herefor brevity.)By contrast, descriptors made by duplication share a common fileposition with the original descriptor that was duplicated.  Anythingwhich alters the file position of one of the duplicates, includingreading or writing data, affects all of them alike.  Thus, for example,@smallexample@{  int d1, d2, d3;  char buf1[4], buf2[4];  d1 = open ("foo", O_RDONLY);  d2 = dup (d1);  d3 = dup (d2);  lseek (d3, 1024, SEEK_SET);  read (d1, buf1, 4);  read (d2, buf2, 4);@}@end smallexample@noindentwill read four characters starting with the 1024'th character of@file{foo}, and then four more characters starting with the 1028'thcharacter.@comment sys/types.h@comment POSIX.1@deftp {Data Type} off_tThis is an arithmetic data type used to represent file sizes.In the GNU system, this is equivalent to @code{fpos_t} or @code{long int}.@end deftpThese aliases for the @samp{SEEK_@dots{}} constants exist for the sakeof compatibility with older BSD systems.  They are defined in twodifferent header files: @file{fcntl.h} and @file{sys/file.h}.@table @code@item L_SETAn alias for @code{SEEK_SET}.@item L_INCRAn alias for @code{SEEK_CUR}.@item L_XTNDAn alias for @code{SEEK_END}.@end table@node Descriptors and Streams@section Descriptors and Streams@cindex streams, and file descriptors@cindex converting file descriptor to stream@cindex extracting file descriptor from streamGiven an open file descriptor, you can create a stream for it with the@code{fdopen} function.  You can get the underlying file descriptor foran existing stream with the @code{fileno} function.  These functions aredeclared in the header file @file{stdio.h}.@pindex stdio.h@comment stdio.h@comment POSIX.1@deftypefun {FILE *} fdopen (int @var{filedes}, const char *@var{opentype})The @code{fdopen} function returns a new stream for the file descriptor@var{filedes}.The @var{opentype} argument is interpreted in the same way as for the@code{fopen} function (@pxref{Opening Streams}), except thatthe @samp{b} option is not permitted; this is because GNU makes nodistinction between text and binary files.  Also, @code{"w"} and@code{"w+"} do not cause truncation of the file; these have affect onlywhen opening a file, and in this case the file has already been opened.You must make sure that the @var{opentype} argument matches the actualmode of the open file descriptor.The return value is the new stream.  If the stream cannot be created(for example, if the modes for the file indicated by the file descriptordo not permit the access specified by the @var{opentype} argument), anull pointer is returned instead.@end deftypefunFor an example showing the use of the @code{fdopen} function,see @ref{Creating a Pipe}.@comment stdio.h@comment POSIX.1@deftypefun int fileno (FILE *@var{stream})This function returns the file descriptor associated with the stream@var{stream}.  If an error is detected (for example, if the @var{stream}is not valid) or if @var{stream} does not do I/O to a file,@code{fileno} returns @code{-1}.@end deftypefun@cindex standard file descriptors@cindex file descriptors, standardThere are also symbolic constants defined in @file{unistd.h} for thefile descriptors belonging to the standard streams @code{stdin},@code{stdout}, and @code{stderr}; see @ref{Standard Streams}.@pindex unistd.h@comment unistd.h@comment POSIX.1@table @code@item STDIN_FILENO@vindex STDIN_FILENOThis macro has value @code{0}, which is the file descriptor forstandard input.@cindex standard input file descriptor@comment unistd.h@comment POSIX.1@item STDOUT_FILENO@vindex STDOUT_FILENOThis macro has value @code{1}, which is the file descriptor forstandard output.@cindex standard output file descriptor@comment unistd.h@comment POSIX.1@item STDERR_FILENO@vindex STDERR_FILENOThis macro has value @code{2}, which is the file descriptor forstandard error output.@end table@cindex standard error file descriptor@node Stream/Descriptor Precautions@section Dangers of Mixing Streams and Descriptors@cindex channels@cindex streams and descriptors@cindex descriptors and streams@cindex mixing descriptors and streamsYou can have multiple file descriptors and streams (let's call bothstreams and descriptors ``channels'' for short) connected to the samefile, but you must take care to avoid confusion between channels.  Thereare two cases to consider: @dfn{linked} channels that share a singlefile position value, and @dfn{independent} channels that have their ownfile positions.It's best to use just one channel in your program for actual datatransfer to any given file, except when all the access is for input.For example, if you open a pipe (something you can only do at the filedescriptor level), either do all I/O with the descriptor, or construct astream from the descriptor with @code{fdopen} and then do all I/O withthe stream.@menu* Linked Channels::	   Dealing with channels sharing a file position.* Independent Channels::   Dealing with separately opened, unlinked channels.* Cleaning Streams::	   Cleaning a stream makes it safe to use                             another channel.@end menu@node Linked Channels@subsection Linked Channels@cindex linked channelsChannels that come from a single opening share the same file position;we call them @dfn{linked} channels.  Linked channels result when youmake a stream from a descriptor using @code{fdopen}, when you get adescriptor from a stream with @code{fileno}, and when you copy adescriptor with @code{dup} or @code{dup2}.  For files that don't supportrandom access, such as terminals and pipes, @emph{all} channels areeffectively linked.  On random-access files, all append-type outputstreams are effectively linked to each other.@cindex cleaning up a streamIf you have been using a stream for I/O, and you want to do I/O usinganother channel (either a stream or a descriptor) that is linked to it,you must first @dfn{clean up} the stream that you have been using.@xref{Cleaning Streams}.Terminating a process, or executing a new program in the process,destroys all the streams in the process.  If descriptors linked to thesestreams persist in other processes, their file positions becomeundefined as a result.  To prevent this, you must clean up the streamsbefore destroying them.@node Independent Channels@subsection Independent Channels@cindex independent channelsWhen you open channels (streams or descriptors) separately on a seekablefile, each channel has its own file position.  These are called@dfn{independent channels}.The system handles each channel independently.  Most of the time, thisis quite predictable and natural (especially for input): each channelcan read or write sequentially at its own place in the file.  However,if some of the channels are streams, you must take these precautions:@itemize @bullet@itemYou should clean an output stream after use, before doing anything elsethat might read or write from the same part of the file.@itemYou should clean an input stream before reading data that may have beenmodified using an independent channel.  Otherwise, you might readobsolete data that had been in the stream's buffer.@end itemizeIf you do output to one channel at the end of the file, this willcertainly leave the other independent channels positioned somewherebefore the new end.  If you want them to output at the end, you must settheir file positions to end of file, first.  (This is not necessary ifyou use an append-type descriptor or stream; they always output at thecurrent end of the file.)  In order to make the end-of-file positionaccurate, you must clean the output channel you were using, if it is astream.  (This is necessary even if you plan to use an append-typechannel next.)It's impossible for two channels to have separate file pointers for afile that doesn't support random access.  Thus, channels for reading orwriting such files are always linked, never independent.  Append-typechannels are also always linked.  For these channels, follow the rulesfor linked channels; see @ref{Linked Channels}.@node Cleaning Streams@subsection Cleaning StreamsOn the GNU system, you can clean up any stream with @code{fclean}:@comment stdio.h@comment GNU@deftypefun int fclean (FILE *@var{stream})Clean up the stream @var{stream} so that its buffer is empty.  If@var{stream} is doing output, force it out.  If @var{stream} is doinginput, give the data in the buffer back to the system, arranging toreread it.@end deftypefunOn other systems, you can use @code{fflush} to clean a stream in mostcases.You can skip the @code{fclean} or @code{fflush} if you know the streamis already clean.  A stream is clean whenever its buffer is empty.  Forexample, an unbuffered stream is always clean.  An input stream that isat end-of-file is clean.  A line-buffered stream is clean when the lastcharacter output was a newline.There is one case in which cleaning a stream is impossible on most

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -