I/O routines input/output API functions

Source: Internet
Author: User
Tags rewind types of functions

Input and Output

The I/O functions read and write data to and from files and devices. file I/O operations take place in text mode or binary mode. the Microsoft run-time Library has three types of I/O functions:

  • Stream I/O functions treat data as a stream of individual characters.

  • Low-level I/O functions invoke the operating system directly for lower-level operation than that provided by stream I/O.
  • Console and port I/O functions read or write directly to a console (keyboard and screen) or an I/O port (such as a printer port ).

WarningBecause stream functions are buffered and low-level functions are not, these two types of functions are generally incompatible. for processing a participant file, use either stream or low-level functions exclusively.

 

Low-level I/O

These functions invoke the operating system directly for lower-level operation than that provided by stream I/O. Low-level input and output CILS do not buffer or format data.

These functions directly access the operating system for low-level operations, unlike stream input and output. Low-level input/output data is not buffered or formatted.

Low-level routines can access the standard streams opened at program startup using the following predefined handles:

Stream Handle
Stdin 0
Stdout 1
Stderr 2

Low-level I/O routines set the errno global variable when an error occurs. you must include stdio. h when you use low-level functions only if your program requires a constant that is defined in stdio. h, such as the end-of-file indicator (EOF).

Low-level I/O functions

Function Use
_ Close Close file
_ Commit Flush file to disk
_ Creat, _ wcreat Create File
_ DUP Return next available file handle for given file
_ Dup2 Create second handle for given file
_ EOF Test for end of File
_ Lseek, _ lseeki64 Reposition file pointer to given location
_ Open, _ wopen Open File
_ Read Read data from File
_ Sopen, _ wsopen Open File for file sharing
_ Tell, _ telli64 Get current file-pointer position
_ Umask Set file-Permission mask
_ Write Write Data to file

_ DUPAnd_ Dup2Are typically used to associate the predefined file handles with different files.

 

Stream I/O

These functions process data in different sizes and formats, from single characters to large data structures. they also provide buffering, which can improve performance. the default size of a stream buffer is 4 K. these routines affect only buffers created by the run-time Library Routines, and have no effect on buffers created by the operating system.

Stream I/O routines

Routine Use
Clearerr Clear error indicator for stream
Fclose Close stream
_ Fcloseall Close all open streams release TStdin,Stdout, AndStderr
_ Fdopen, wfdopen Associate stream with handle to open file
Feof Test for end of file on stream
Ferror Test for error on stream
Fflush Flush stream to buffer or storage device
Fgetc, fgetwc Read character from stream (function versionsGETCAndGetwc)
_ Fgetchar, _ fgetwchar Read character fromStdin(Function versionsGetcharAndGetwchar)
Fgetpos Get position indicator of stream
Fgets, fgetws Read string from stream
_ Fileno Get file handle associated with stream
_ Flushall Flush all streams to buffer or storage device
Fopen, _ wfopen Open stream
Fprintf, fwprintf Write formatted data to stream
Fputc, fputwc Write a character to a stream (function versionsPutcAndPutwc)
_ Fputchar, _ fputwchar Write characterStdout(Function versionsPutcharAndPutwchar)
Fputs, fputws Write string to stream
Fread Read unformatted data from stream
Freopen, _ wfreopen ReassignFileStream pointer to new file or device
Fscanf, fwscanf Read formatted data from stream
Fseek Move file position to given location
Fsetpos Set position indicator of stream
_ Fsopen, _ wfsopen Open stream with file sharing
Ftell Get current file position
Fwrite Write unformatted data items to stream
GETC, getwc Read character from stream (macro versionsFgetcAndFgetwc)
Getchar, getwchar Read character fromStdin(Macro versionsFgetcharAndFgetwchar)
Gets, getws Read line fromStdin
_ Getw Read BinaryIntFrom stream
Printf, wprintf Write formatted dataStdout
Putc, putwc Write character to a stream (macro versionsFputcAndFputwc)
Putchar, putwchar Write characterStdout(Macro versionsFputcharAndFputwchar)
Puts, _ putws Write line to stream
_ Putw Write binaryIntTo stream
Rewind Move file position to beginning of stream
_ Rmtmp Remove temporary files createdTmpfile
Scanf, wscanf Read formatted data fromStdin
Setbuf Control Stream Buffering
_ Setmaxstdio Set a maximum for the number of simultaneously open files at the stream I/O level.
Setvbuf Control Stream buffering and buffer size
_ Snprintf, _ snwprintf Write formatted data of specified length to string
Sprintf, swprintf Write formatted data to string
Sscanf, swscanf Read formatted data from string
_ Tempnam, _ wtempnam Generate temporary filename in given directory
Tmpfile Create temporary file
Tmpnam, _ wtmpnam Generate temporary filename
Ungetc, ungetwc Push character back onto stream
Vfprintf, vfwprintf Write formatted data to stream
Vprintf, vwprintf Write formatted dataStdout
_ Vsnprintf, _ vsnwprintf Write formatted data of specified length to buffer
Vsprintf, vswprintf Write formatted data to buffer

When a program begins execution, the startup code automatically opens several streams: standard input (pointed toStdin), Standard output (pointed toStdout), And standard error (pointed toStderr). These streams are directed to the console (keyboard and screen) by default. UseFreopenTo redirectStdin,Stdout, OrStderrTo a disk file or a device.

Files opened using the stream routines are buffered by default.StdoutAndStderrFunctions are flushed whenever they are full or, if you are writing to a character device, after each library call. if a program terminates abnormally, output buffers may not be flushed, resulting in loss of data. useFflushOr_ FlushallTo ensure that the buffer associated with a specified file or all open buffers are flushed to the operating system, which can cache data before writing it to disk. the commit-to-disk feature ensures that the flushed buffer contents are not lost in the event of a system failure.

There are two ways to commit buffer contents to disk:

  • Link with the file commode. OBJ to set a global commit flag. The default setting of the global flag isN, For "no-commit ."

  • Set the mode flagCWithFopenOr_ Fdopen.

Any file specifically opened with eitherCOrNFlag behaves according to the flag, regardless of the state of the global commit/no-commit flag.

If your program does not explicitly close a stream, the stream is automatically closed when the program terminates. however, you shoshould close a stream when your program finishes with it, as the number of streams that can be open at one time is limited. see _ setmaxstdio for information on this limit.

Input can follow output directly only with an intervening callFflushOr to a file-positioning function (Fseek,Fsetpos, OrRewind). Output can follow input without an intervening call to a file-positioning function if the input operation encounters the end of the file.

From: http://www.linuxtopia.org/online_books/programming_books/gnu_libc_guide/Low_002dLevel-I_002fO.html

Stream-Level I/O is more flexible and usually more convenient; therefore, Programmers Generally use the descriptor-level functions only when necessary. These are some of the usual reasons:

  • For reading binary files in large chunks.
  • For reading an entire file Core Before parsing it.
  • To perform operations other than data transfer, which can only be done with a descriptor. (You can usefilenoTo get the descriptor corresponding to a stream .)
  • To pass descriptors to a child process. (The child can create its own stream to use a descriptor that it inherits, but cannot inherit a stream directly .)

  • Opening and Closing files: how to open and close file descriptors.
  • I/O primitives: Reading and Writing data.
  • File position primitive: setting a descriptor's file position.
  • Descriptors and streams: Converting descriptor to stream or vice-versa.
  • Stream/descriptor precautions: precautions needed if you use both descriptors and streams.
  • Scatter-gather: fast I/O to discontinuous buffers.
  • Memory-mapped I/O: Using files like Memory .
  • Waiting for I/O: How to check for input or output on multiple file descriptors.
  • Synchronizing I/O: making sure all I/O Actions completed.
  • Asynchronous I/O: perform I/O in parallel.
  • Control Operations: varous other operations on file descriptors.
  • Duplicating descriptors: fcntl Commands For duplicating file descriptors.
  • Descriptor flags: fcntl Commands For manipulating flags associated with file descriptors.
  • File Status flags: fcntl commands for manipulating flags associated with open files.
  • File locks: fcntl commands for implementing File Locking.
  • Interrupt input: getting an asynchronous signal when input arrives.
  • IOCTLs: Generic I/O control operations.

    Other articles about lowlevel I/O:

    Http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_8.html
    Http://www.gnu.org/software/libc/manual/html_node/Low_002dLevel-I_002fO.html
    Http://www.informit.com/guides/content.aspx? G = cplusplus & seqnum = 208
    Http://www.thinkage.ca/english/gcos/expl/c/lib/open.html

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.