Read Number of Bytes From File C
The C programming language provides many standard library functions for file input and output. These functions make upward the bulk of the C standard library header <stdio.h>.[1] The functionality descends from a "portable I/O parcel" written by Mike Lesk at Bell Labs in the early 1970s,[2] and officially became function of the Unix operating system in Version 7.[3]
The I/O functionality of C is fairly depression-level by modern standards; C abstracts all file operations into operations on streams of bytes, which may be "input streams" or "output streams". Unlike some earlier programming languages, C has no direct support for random-access information files; to read from a record in the middle of a file, the programmer must create a stream, seek to the middle of the file, and then read bytes in sequence from the stream.
The stream model of file I/O was popularized past Unix, which was developed concurrently with the C programming language itself. The vast majority of modernistic operating systems have inherited streams from Unix, and many languages in the C programming language family have inherited C'south file I/O interface with few if whatsoever changes (for example, PHP).
Overview [edit]
This library uses what are called streams to operate with physical devices such as keyboards, printers, terminals or with any other type of files supported by the organisation. Streams are an brainchild to interact with these in a compatible style. All streams have like properties independent of the individual characteristics of the concrete media they are associated with.[4]
Functions [edit]
Most of the C file input/output functions are defined in <stdio.h> (or in the C++ header cstdio, which contains the standard C functionality but in the std namespace).
Byte grapheme | Wide graphic symbol | Description | |
---|---|---|---|
File access | fopen | Opens a file (with a not-Unicode filename on Windows and possible UTF-viii filename on Linux) | |
freopen | Opens a unlike file with an existing stream | ||
fflush | Synchronizes an output stream with the actual file | ||
fclose | Closes a file | ||
setbuf | Sets the buffer for a file stream | ||
setvbuf | Sets the buffer and its size for a file stream | ||
fwide | Switches a file stream betwixt wide-character I/O and narrow-grapheme I/O | ||
Direct input/output | fread | Reads from a file | |
fwrite | Writes to a file | ||
Unformatted input/output | fgetc getc | fgetwc getwc | Reads a byte/wchar_t from a file stream |
fgets | fgetws | Reads a byte/wchar_t line from a file stream | |
fputc putc | fputwc putwc | Writes a byte/wchar_t to a file stream | |
fputs | fputws | Writes a byte/wchar_t cord to a file stream | |
getchar | getwchar | Reads a byte/wchar_t from stdin | |
| N/A | Reads a byte string from stdin until a newline or stop of file is encountered (deprecated in C99, removed from C11) | |
putchar | putwchar | Writes a byte/wchar_t to stdout | |
puts | Due north/A | Writes a byte string to stdout | |
ungetc | ungetwc | Puts a byte/wchar_t back into a file stream | |
Formatted input/output | scanf fscanf sscanf | wscanf fwscanf swscanf | Reads formatted byte/wchar_t input from stdin, a file stream or a buffer |
vscanf vfscanf vsscanf | vwscanf vfwscanf vswscanf | Reads formatted input byte/wchar_t from stdin, a file stream or a buffer using variable argument list | |
printf fprintf sprintf snprintf | wprintf fwprintf swprintf | Prints formatted byte/wchar_t output to stdout, a file stream or a buffer | |
vprintf vfprintf vsprintf vsnprintf | vwprintf vfwprintf vswprintf | Prints formatted byte/wchar_t output to stdout, a file stream, or a buffer using variable argument list | |
perror | N/A | Writes a description of the current fault to stderr | |
File positioning | ftell ftello | Returns the current file position indicator | |
fseek fseeko | Moves the file position indicator to a specific location in a file | ||
fgetpos | Gets the file position indicator | ||
fsetpos | Moves the file position indicator to a specific location in a file | ||
rewind | Moves the file position indicator to the outset in a file | ||
Mistake handling | clearerr | Clears errors | |
feof | Checks for the end-of-file | ||
ferror | Checks for a file mistake | ||
Operations on files | remove | Erases a file | |
rename | Renames a file | ||
tmpfile | Returns a pointer to a temporary file | ||
tmpnam | Returns a unique filename |
Constants [edit]
Constants defined in the <stdio.h> header include:
Name | Notes |
---|---|
EOF | A negative integer of type int used to bespeak end-of-file conditions |
BUFSIZ | An integer which is the size of the buffer used by the setbuf() function |
FILENAME_MAX | The size of a char array which is large enough to store the proper noun of any file that tin can be opened |
FOPEN_MAX | The number of files that may be open simultaneously; will be at to the lowest degree viii |
_IOFBF | An abridgement for "input/output fully buffered"; information technology is an integer which may exist passed to the setvbuf() function to asking block buffered input and output for an open up stream |
_IOLBF | An abbreviation for "input/output line buffered"; it is an integer which may be passed to the setvbuf() function to request line buffered input and output for an open stream |
_IONBF | An abbreviation for "input/output not buffered"; it is an integer which may be passed to the setvbuf() function to asking unbuffered input and output for an open stream |
L_tmpnam | The size of a char array which is big enough to store a temporary filename generated by the tmpnam() function |
Cypher | A macro expanding to the nil pointer constant; that is, a constant representing a pointer value which is guaranteed not to exist a valid address of an object in retention |
SEEK_CUR | An integer which may exist passed to the fseek() function to request positioning relative to the electric current file position |
SEEK_END | An integer which may exist passed to the fseek() role to request positioning relative to the end of the file |
SEEK_SET | An integer which may exist passed to the fseek() part to request positioning relative to the beginning of the file |
TMP_MAX | The maximum number of unique filenames generable past the tmpnam() function; volition be at least 25 |
Variables [edit]
Variables defined in the <stdio.h> header include:
Name | Notes |
---|---|
stdin | A arrow to a FILE which refers to the standard input stream, usually a keyboard. |
stdout | A pointer to a FILE which refers to the standard output stream, normally a display concluding. |
stderr | A pointer to a FILE which refers to the standard fault stream, often a brandish terminal. |
Member types [edit]
Information types defined in the <stdio.h> header include:
- FILE – as well known as a file handle, this is an opaque blazon containing the data most a file or text stream needed to perform input or output operations on it, including:
- platform-specific identifier of the associated I/O device, such as a file descriptor
- the buffer
- stream orientation indicator (unset, narrow, or broad)
- stream buffering land indicator (unbuffered, line buffered, fully buffered)
- I/O mode indicator (input stream, output stream, or update stream)
- binary/text fashion indicator
- end-of-file indicator
- fault indicator
- the current stream position and multibyte conversion state (an object of type mbstate_t)
- reentrant lock (required as of C11)
- fpos_t – a non-array type capable of uniquely identifying the position of every byte in a file and every conversion land that can occur in all supported multibyte graphic symbol encodings
- size_t – an unsigned integer type which is the type of the issue of the sizeof operator.
Extensions [edit]
The POSIX standard defines several extensions to stdio in its Base Definitions, among which are a readline function that allocates memory, the fileno and fdopen functions that plant the link between FILE objects and file descriptors, and a group of functions for creating FILE objects that refer to in-memory buffers.[5]
Example [edit]
The post-obit C program opens a binary file called myfile, reads five bytes from it, and and then closes the file.
#include <stdio.h> #include <stdlib.h> int principal ( void ) { char buffer [ 5 ]; FILE * fp = fopen ( "myfile" , "rb" ); if ( fp == Null ) { perror ( "Failed to open file \" myfile \" " ); return EXIT_FAILURE ; } for ( int i = 0 ; i < five ; i ++ ) { int rc = getc ( fp ); if ( rc == EOF ) { fputs ( "An error occurred while reading the file. \n " , stderr ); return EXIT_FAILURE ; } buffer [ i ] = rc ; } fclose ( fp ); printf ( "The bytes read were... %x %x %x %x %ten \northward " , buffer [ 0 ], buffer [ 1 ], buffer [ two ], buffer [ 3 ], buffer [ 4 ]); render EXIT_SUCCESS ; }
Alternatives to stdio [edit]
Several alternatives to stdio have been developed. Amongst these is the C++ iostream library, part of the ISO C++ standard. ISO C++ still requires the stdio functionality.
Other alternatives include the SFIO[6] (A Safe/Fast I/O Library) library from AT&T Bell Laboratories. This library, introduced in 1991, aimed to avert inconsistencies, dangerous practices and inefficiencies in the blueprint of stdio. Among its features is the possibility to insert callback functions into a stream to customize the handling of information read from or written to the stream.[7] It was released to the outside world in 1997, and the final release was 1 February 2005.[8]
See also [edit]
- printf format cord
- scanf format cord
References [edit]
- ^ ISO/IEC 9899:1999 specification (PDF). p. 274, § 7.nineteen.
- ^ Kernighan, Brian; State highway, Rob (1984). The UNIX Programming Surround. Englewood Cliffs: Prentice Hall. p. 200.
- ^ McIlroy, M. D. (1987). A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 (PDF) (Technical report). CSTR. Bell Labs. 139.
- ^ "(stdio.h) - C++ Reference".
- ^ – Base Definitions Reference, The Single UNIX Specification, Upshot 7 from The Open Grouping
- ^ "SFIO: A Safe/Fast I/O Library". Archived from the original on eleven February 2006. Retrieved 16 March 2021.
{{cite web}}
: CS1 maint: bot: original URL condition unknown (link) - ^ Korn, David G.; Vo, Kiem-Phong (1991). SFIO: Prophylactic/Fast Cord/File IO. Proc. Summer USENIX Conf. CiteSeerX10.i.1.51.6574.
- ^ Fowler, Glenn S.; Korn, David G.; Vo, Kiem-Phong (2000). Extended Formatting with Sfio. Proc. Summer USENIX Conf.
External links [edit]
-
Media related to C file input/output at Wikimedia Eatables
Source: https://en.wikipedia.org/wiki/C_file_input/output
0 Response to "Read Number of Bytes From File C"
Post a Comment