C file input/output: Difference between revisions

Content deleted Content added
No edit summary
Tags: Mobile edit Mobile app edit iOS app edit App section source
 
(38 intermediate revisions by 25 users not shown)
Line 1:
{{short description|Input/output functionality in the C programming language}}
{{useUse dmy dates|date=JanuaryFebruary 20122022}}
{{C Standard Library}}
The [[C (programming language)|C programming language]] provides many [[standard library]] [[subroutine|functions]] for [[computer file|file]] [[input/output|input and output]]. These functions make up the bulk of the [[C standard library]] [[header file|header]] {{mono|<'''stdio.h'''>}}.<ref>{{cite book |url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf |title=ISO/IEC 9899:1999 specification |at=p. 274, § 7.19 |language=en-US}}</ref> The functionality descends from a "portable I/O package" written by [[Mike Lesk]] at [[Bell Labs]] in the early 1970s,<ref>{{cite book |last1=Kernighan |first1=Brian |author-link1=Brian Kernighan |last2=Pike |first2=Rob |author-link2=Rob Pike |title=[[The UNIX Programming Environment]] |publisher=[[Prentice Hall]] |___location=[[Englewood Cliffs]] |year=1984 |page=200|bibcode=1984upe..book.....K }}</ref> and officially became part of the [[Unix]] operating system in [[Version 7 Unix|Version 7]].<ref name="reader">{{cite techreporttech report |first1=M. D. |last1=McIlroy |author-link1=Doug McIlroy |year=1987 |url=http://www.cs.dartmouth.edu/~doug/reader.pdf |title=A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 |series=CSTR |number=139 |institution=Bell Labs}}</ref>
 
The I/O functionality of C is fairly low-level by modern standards; C abstracts all file operations into operations on [[stream (computing)|stream]]s of [[byte]]s, which may be "input streams" or "output streams". Unlike some earlier programming languages, C has no direct support for [[random access|random-access]] data files; to read from a record in the middle of a file, the programmer must create a stream, [[fseek|seek]] to the middle of the file, and then read bytes in sequence from the stream.
Line 8 ⟶ 9:
 
==Overview==
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 system. Streams are an abstraction to interact with these in a uniform way. All streams have similar properties independent of the individual characteristics of the physical media they are associated with.<ref>{{Cite web |title=(stdio.h) - C++ Reference |url=http://www.cplusplus.com/reference/cstdio/ |access-date=July 25, 2021 |website=C++ |language=en-US}}</ref>
 
===Functions===
Most of the C file input/output functions are defined in {{mono|<stdio.h>}} (or in the [[C++]] header {{mono|cstdio}}, which contains the standard C functionality but in the {{mono|std}} [[namespace]]).
 
{| class="wikitable"
Line 20 ⟶ 21:
! Description
|-
! rowspan=79 | File access
| colspan=2 style="text-align:center;font-family:monospace" | {{anchor|fopen}}[https://en.cppreference.com/w/c/io/fopen fopen]
| Opens a file (with a non-Unicode filename on Windows and possible UTF-8 filename on Linux)
|-
| colspan=2 style="text-align:center;font-family:monospace" | {{anchor|popen}}[https://man7.org/linux/man-pages/man3/popen.3.html popen]
| opens a process by creating a pipe, forking, and invoking the shell
|-
| colspan=2 style="text-align:center;font-family:monospace" | {{anchor|freopen}}[https://en.cppreference.com/w/c/io/freopen freopen]
Line 32 ⟶ 36:
| colspan=2 style="text-align:center;font-family:monospace" | {{anchor|fclose}}[https://en.cppreference.com/w/c/io/fclose fclose]
| Closes a file
|-
| colspan=2 style="text-align:center;font-family:monospace" | {{anchor|pclose}}[https://man7.org/linux/man-pages/man3/pclose.3p.html pclose]
| closes a stream
|-
| colspan=2 style="text-align:center;font-family:monospace" | {{anchor|setbuf}}[https://en.cppreference.com/w/c/io/setbuf setbuf]
Line 92 ⟶ 99:
|-
| style="font-family:monospace" | {{anchor|vscanf|vfscanf|vsscanf}}[https://en.cppreference.com/w/c/io/vscanf vscanf]<br />[https://en.cppreference.com/w/c/io/vfscanf vfscanf]<br />[https://en.cppreference.com/w/c/io/vsscanf vsscanf]
| style="font-family:monospace" | {{anchor|vwscanf|vfwscanf|svwscanfvswscanf}}[https://en.cppreference.com/w/c/io/vwscanf vwscanf]<br />[https://en.cppreference.com/w/c/io/vfwscanf vfwscanf]<br />[https://en.cppreference.com/w/c/io/vswscanf vswscanf]
| Reads formatted input byte/{{mono|wchar_t}} from stdin,<br />a file stream or a buffer using variable argument list
|-
Line 99 ⟶ 106:
| Prints formatted byte/{{mono|wchar_t}} output to stdout,<br />a file stream or a buffer
|-
| style="font-family:monospace" | {{anchor|vprintf|vfprintf|vspritfvsprintf|vsnprintf}}[https://en.cppreference.com/w/c/io/vprintf vprintf]<br />[https://en.cppreference.com/w/c/io/vfprintf vfprintf]<br />[https://en.cppreference.com/w/c/io/vsprintf vsprintf]<br />[https://en.cppreference.com/w/c/io/vsnprintf vsnprintf]
| style="font-family:monospace" | {{anchor|vwprintf|vfwprintf|vswprintf}}[https://en.cppreference.com/w/c/io/vwprintf vwprintf]<br />[https://en.cppreference.com/w/c/io/vfwprintf vfwprintf]<br />[https://en.cppreference.com/w/c/io/vfwprintf vswprintf]
| Prints formatted byte/{{mono|wchar_t}} output to stdout,<br />a file stream, or a buffer using variable argument list
Line 157 ⟶ 164:
| A negative integer of type {{mono|int}} used to indicate end-of-file conditions
|-
! style="font-family:monospace" | {{Anchor|BUFSIZ}} [http://c-p-p.net/c/stdio.h/bufsiz BUFSIZ]
| An integer which is the size of the buffer used by the {{mono|setbuf()}} function
|-
Line 214 ⟶ 221:
===Member types===
Data types defined in the {{mono|<stdio.h>}} header include:
*{{mono|[https://en.cppreference.com/w/c/io FILE]}} – also known as a {{anchor|file handle}}'''file [[Handle (computing)|handle]]''' or a '''{{Visible anchor|FILE pointer}}''', this is an [[opaque pointer|opaque type]] containing the information about 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
Line 240 ⟶ 247:
int main(void) {
char buffer[5];
size_t len;
FILE* fp = fopen("myfile", "rb");
 
Line 247 ⟶ 255:
}
 
forif (int i(len = 0;fread(buffer, i <1, 5;, i++fp)) < 0) {
int rc = getcfclose(fp);
fputs("An error occurred while reading the file.\n", stderr);
if (rc == EOF) {
return EXIT_FAILURE;
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 %x\n", buffer[0], buffer[1],);
for (int i = 0; i < len; ++i) {
buffer[2], buffer[3], buffer[4]);
printf("%02X ", buffer[i] = rc);
}
putchar('\n');
 
return EXIT_SUCCESS;
Line 267 ⟶ 274:
 
==Alternatives to stdio{{anchor|Sfio}}==
{{Redirect|Sfio|other uses of "SFIO"|SFIO (disambiguation)}}
 
Several alternatives to {{mono|stdio}} have been developed. Among these is theare [[Input/output (C++)|C++ {{mono|iostream}}I/O]] libraryheaders <code><iostream></code> and <code><print></code>, part of the [[ISO C++|ISO C++ standard]]. ISO C++ still requires the {{mono|stdio}} functionality.
{{Redirect|Sfio|the defunct French political party|French Section of the Workers' International}}
 
Several alternatives to {{mono|stdio}} have been developed. Among these is the [[Input/output (C++)|C++ {{mono|iostream}}]] library, part of the [[ISO C++|ISO C++ standard]]. ISO C++ still requires the {{mono|stdio}} functionality.
 
Other alternatives include the SFIOSfio<ref>{{webarchiveCite web |url=http://akpublic.research.att.com/sw/tools/sfio/ |title=Sfio: A Safe/Fast I/O Library |access-date=16 March 2021 |archive-date=11 February 2006 |archive-url=https://web.archive.org/web/20060211021834/http://akpublic.research.att.com/sw/tools/sfio/ |titleurl-status=SFIObot: Aunknown Safe/Fast I/O Library}}</ref> (A Safe/Fast I/O Library) library from [[AT&T Bell Laboratories]]. This library, introduced in 1991, aimed to avoid inconsistencies, unsafe practices and inefficiencies in the design of {{mono|stdio}}. Among its features is the possibility to insert [[callback function]]s into a stream to customize the handling of data read from or written to the stream.<ref>{{cite conference |title=SFIO: Safe/Fast String/File IO |first1=David G. |last1=Korn |author-link=David Korn (computer scientist) |first2=Kiem-Phong |last2=Vo |conference=Proc. Summer USENIX Conf. |year=1991 |citeseerx=10.1.1.51.6574}}</ref> It was released to the outside world in 1997, and the last release was 200502011 February 2005.<ref>{{cite conference |first1=Glenn S. |last1=Fowler |first2=David G. |last2=Korn |first3=Kiem-Phong |last3=Vo |title=Extended Formatting with Sfio |conference=Proc. Summer USENIX Conf. |year=2000}}</ref>
 
==See also==
Line 283 ⟶ 289:
==External links==
{{wikibooks|C Programming|C file input and output|C Programming/C Reference}}
*{{Commons category-inline}}
*{{Commonscatinline}}
 
{{CProLang}}
{{use dmy dates|date=January 2012}}
 
{{DEFAULTSORT:C file input output}}