C file input/output: Difference between revisions

Content deleted Content added
mNo edit summary
adding table and note about "b"
Line 12:
The '''mode''' parameter is a string that begins with one of the following sequences:
 
{| class="wikitable"
r Open a text file for reading
|-
w Truncate file to zero length or create text file for writing
!colspan=3| mode || description
a Append: open or create text file for writing at end-of-file
|-
rb Open a binary file for reading
|r || rb || || open for reading
wb Truncate file to zero length or create binary file for writing
|-
ab Append: open or create binary file for writing at end-of-file
|w || wb || || open for writing (file need not exist)
r+ Open text file for update (reading and writing)
|-
w+ Truncate file to zero length or create text file for update
|a+ || ab || Append:|| open orfor create textappending (file forneed update,not writing at end-of-fileexist)
|-
|r+b or|| rb+ Open|| binaryr+b file|| open for update (reading and writing), start at beginning
w+b or wb+ Truncate file to zero length or create binary file for update
|-
a+b or ab+ Append: open or create binary file for update, writing at end-of-file
|w+ || wb+ || w+b || open for reading and writing (overwrite file)
|-
|a+ || ab+ || a+b || open for reading and writing (append if file exists)
|}
 
The 'b' has no affect on the operation, but is included for clarity. It stands for binary. [http://www.opengroup.org/onlinepubs/009695399/functions/fopen.html]
The C standard gives two kinds of files — text files and binary files — 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.
 
The C standard gives two kinds of files —- text files and binary files — 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, but non-standard, way to find the length of a file in C is: