C file input/output: Difference between revisions

Content deleted Content added
No edit summary
remove faulty "example" from end of article, modify first P
Line 1:
{{wrongtitle|title="fopen"}}
 
In [[computer programming]], the <code>'''fopen'''</code>, with related functions <code>'''fdopen'''</code> and '''<code>freopen</code>''',function is one of the basic functions in the [[C programming language]]. and also exists in various other computer languagesIt suchreturns asan [[PHPI/O]]. They return a [[stream]] attached to the specified [[computer file|file]] or other device from which reading and writing can be done. Because itthe functionality is so popularuseful, many script languages derived from C tendprovide tofunctions provideof the same functionname, with the same or similar functionalityfunction: for example, [[PHP]]. It<code>fopen</code> is considered higher-level than the <code>[[open]]</code> [[system call]] of UNIX operating systems. The related C library function '''<code>freopen</code>''' performs the same operation after first closing any open stream associated with its parameter.
 
They are defined as.
 
They are defined as.
:<code>FILE * fopen(const char *path, const char *mode);</code>
:<code>FILE * fdopen(int fildes, const char *mode);</code>
:<code>FILE * freopen(const char *path, const char *mode, FILE * restrict stream);</code>
 
The '''<code>fdopen</code>''' function is not standard in C89 or C99, but is an extension used in [[POSIX]] environments and imitated elsewhere.
 
The '''mode''' parameter is a string that begins with one of the following sequences:
Line 26 ⟶ 25:
a+b or ab+ Append: open or create binary file for update, writing at end-of-file
 
The C standard gives two kinds of files: &mdash; text files and binary files, &mdash; although operating systems may or may not distinguish between the two. A ''text file'' is a file consisting of text arranged in lines with some sort of distinguishing end-of-line character or sequence (in [[Unix]], a bare linefeed character; in the [[Apple Macintosh|Macintosh]] OS, a bare carriage return; on [[DOS]] and [[Microsoft Windows]], a carriage return followed by a linefeed). When bytes are read in from a text file, an end-of-line sequence is usually mapped to a linefeed for ease in processing. When a text file is written to, a bare linefeed is mapped to the OS-specific end-of-line character sequence before writing. A ''binary file'' is a file where bytes are read in "raw"," and delivered "raw"," without any kind of mapping.
 
A convenient way to find the length of a file in C is:
<pre><nowiki>
FILE *f = fopen("filename", "rb");
fseek(f, 0, SEEK_END);
length = tell(f);
rewind(f);
</nowiki></pre>
 
[[Category:C standard library]]