fopen 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. .TH FOPEN 2
  2. .SH NAME
  3. fopen, freopen, fdopen, fileno, fclose, sopenr, sopenw, sclose, fflush, setvbuf, setbuf, fgetpos, ftell, fsetpos, fseek, rewind, feof, ferror, clearerr \- standard buffered input/output package
  4. .SH SYNOPSIS
  5. .B #include <stdio.h>
  6. .PP
  7. .ta \w'\fLFILE 'u
  8. .B
  9. FILE *fopen(char *filename, char *mode)
  10. .PP
  11. .B
  12. FILE *freopen(char *filename, char *mode, FILE *f)
  13. .PP
  14. .B
  15. FILE *fdopen(int fd, char *mode)
  16. .PP
  17. .B
  18. int fileno(FILE *f)
  19. .PP
  20. .B
  21. FILE *sopenr(char *s)
  22. .PP
  23. .B
  24. FILE *sopenw(void)
  25. .PP
  26. .B
  27. char *sclose(FILE *f)
  28. .PP
  29. .B
  30. int fclose(FILE *f)
  31. .PP
  32. .B
  33. int fflush(FILE *f)
  34. .PP
  35. .B
  36. int setvbuf(FILE *f, char *buf, int type, long size)
  37. .PP
  38. .B
  39. void setbuf(FILE *f, char *buf)
  40. .PP
  41. .B
  42. int fgetpos(FILE *f, long *pos)
  43. .PP
  44. .B
  45. long ftell(FILE *f)
  46. .PP
  47. .B
  48. int fsetpos(FILE *f, long *pos)
  49. .PP
  50. .B
  51. int fseek(FILE *f, long offset, int whence)
  52. .PP
  53. .B
  54. void rewind(FILE *f)
  55. .PP
  56. .B
  57. int feof(FILE *f)
  58. .PP
  59. .B
  60. int ferror(FILE *f)
  61. .PP
  62. .B
  63. void clearerr(FILE *f)
  64. .SH DESCRIPTION
  65. The functions described in this and related pages
  66. .RI ( fgetc (2),
  67. .IR fprintf (2),
  68. .IR fscanf (2),
  69. and
  70. .IR tmpfile (2))
  71. implement the
  72. ANSI C buffered I/O package with extensions.
  73. .PP
  74. A file with associated buffering is called a
  75. .I stream
  76. and is declared to be a pointer to a defined type
  77. .BR FILE .
  78. .IR Fopen (2)
  79. creates certain descriptive data for a stream
  80. and returns a pointer to designate the stream in all
  81. further transactions.
  82. There are three normally open streams with constant
  83. pointers declared in
  84. the include file and associated with the standard open files:
  85. .TP 10n
  86. .BR stdin
  87. standard input file
  88. .br
  89. .ns
  90. .TP
  91. .B stdout
  92. standard output file
  93. .br
  94. .ns
  95. .TP
  96. .B stderr
  97. standard error file
  98. .PP
  99. A constant pointer
  100. .B NULL
  101. designates no stream at all.
  102. .PP
  103. .I Fopen
  104. opens the file named by
  105. .I filename
  106. and associates a stream with it.
  107. .I Fopen
  108. returns a pointer to be used to identify
  109. the stream in subsequent operations, or
  110. .B NULL
  111. if the open fails.
  112. .I Mode
  113. is a character string having one of the following values:
  114. .nf
  115. .ta 8n
  116. \fL"r"\fP open for reading
  117. \fL"w"\fP truncate to zero length or create for writing
  118. \fL"a"\fP append; open or create for writing at end of file
  119. \fL"r+"\fP open for update (reading and writing)
  120. \fL"w+"\fP truncate to zero length or create for update
  121. \fL"a+"\fP append; open or create for update at end of file
  122. .fi
  123. .PP
  124. In addition, each of the above strings can have a
  125. .L b
  126. somewhere after the first character,
  127. meaning `binary file', but this implementation makes no distinction
  128. between binary and text files.
  129. .PP
  130. .I Fclose
  131. causes the stream pointed to by
  132. .I f
  133. to be flushed (see below) and does a
  134. .I close
  135. (see
  136. .IR open (2))
  137. on the associated file.
  138. It frees any automatically allocated buffer.
  139. .I Fclose
  140. is called automatically on
  141. .IR exits (2)
  142. for all open streams.
  143. .PP
  144. .I Freopen
  145. is like open except that it reuses stream pointer
  146. .IR f .
  147. .I Freopen
  148. first attempts to close any file associated with
  149. .IR f ;
  150. it ignores any errors in that close.
  151. .PP
  152. .I Fdopen
  153. associates a stream with an open Plan 9 file descriptor.
  154. .PP
  155. .I Fileno
  156. returns the number of the Plan 9 file descriptor associated with the stream.
  157. .PP
  158. .I Sopenr
  159. associates a read-only stream with a null-terminated string.
  160. .PP
  161. .I Sopenw
  162. opens a stream for writing.
  163. No file descriptor is associated with the stream;
  164. instead, all output is written to the stream buffer.
  165. .PP
  166. .I Sclose
  167. closes a stream opened with
  168. .I sopenr
  169. or
  170. .IR sopenw .
  171. It returns a pointer to the 0 terminated buffer associated with the stream.
  172. .PP
  173. By default, output to a stream is fully buffered: it is accumulated in
  174. a buffer until the buffer is full, and then
  175. .I write
  176. (see
  177. .IR read (2))
  178. is used to write the buffer.
  179. An exception is standard error, which is line buffered:
  180. output is accumulated in a buffer until
  181. a newline is written.
  182. Input is also fully buffered by default; this means that
  183. .IR read (2)
  184. is used to fill a buffer as much as it can, and then characters
  185. are taken from that buffer until it empties.
  186. .I Setvbuf
  187. changes the buffering method for file
  188. .I f
  189. according to
  190. .IR type:
  191. either
  192. .B _IOFBF
  193. for fully buffered,
  194. .B _IOLBF
  195. for line buffered,
  196. or
  197. .B _IONBF
  198. for unbuffered (each character causes a
  199. .I read
  200. or
  201. .IR write).
  202. If
  203. .I buf
  204. is supplied, it is used as the buffer and
  205. .I size
  206. should be its size;
  207. If
  208. .I buf
  209. is zero, a buffer of the given size is allocated (except for the unbuffered case) using
  210. .IR malloc (2).
  211. .PP
  212. .I Setbuf
  213. is an older method for changing buffering. If
  214. .I buf
  215. is supplied, it changes to fully buffered with the given buffer, which should
  216. be of size
  217. .B BUFSIZ
  218. (defined in
  219. .BR stdio.h ).
  220. If
  221. .I buf
  222. is zero, the buffering method changes to unbuffered.
  223. .PP
  224. .I Fflush
  225. flushes the buffer of output stream
  226. .IR f ,
  227. delivering any unwritten buffered data to the host file.
  228. .PP
  229. There is a
  230. .I file position indicator
  231. associated with each stream.
  232. It starts out pointing at the first character (unless the file is opened
  233. with append mode, in which case the indicator is always ignored).
  234. The file position indicator is maintained by the reading and writing
  235. functions described in
  236. .IR fgetc (2).
  237. .PP
  238. .I Fgetpos
  239. stores the current value of the file position indicator for stream
  240. .I f
  241. in the object pointed to by
  242. .IR pos .
  243. It returns zero on success, nonzero otherwise.
  244. .I Ftell
  245. returns the current value of the file position indicator.
  246. The file position indicator is to
  247. be used only as an argument to
  248. .IR fseek.
  249. .PP
  250. .I Fsetpos
  251. sets the file position indicator for stream
  252. .I f
  253. to the value of the object pointed to by
  254. .IR pos ,
  255. which shall be a value returned by an earlier call to
  256. .I fgetpos
  257. on the same stream.
  258. It returns zero on success, nonzero otherwise.
  259. .I Fseek
  260. obtains a new position, measured in characters from the beginning
  261. of the file, by adding
  262. .I offset
  263. to the position specified by
  264. .IR whence :
  265. the beginning of the file if
  266. .I whence
  267. is
  268. .BR SEEK_SET ;
  269. the current value of the file position indicator for
  270. .BR SEEK_CUR ;
  271. and the end-of-file for
  272. .BR SEEK_END .
  273. .I Rewind
  274. sets the file position indicator to the beginning of the file.
  275. .PP
  276. An integer constant
  277. .B EOF
  278. is returned
  279. upon end of file or error by integer-valued functions that
  280. deal with streams.
  281. .I Feof
  282. returns non-zero if and only if
  283. .I f
  284. is at its end of file.
  285. .PP
  286. .I Ferror
  287. returns non-zero if and only if
  288. .I f
  289. is in the error state. It can get into the error state
  290. if a system call failed on the associated file
  291. or a memory allocation failed.
  292. .I Clearerr
  293. takes a stream out of the error state.
  294. .SH SOURCE
  295. .B /sys/src/libstdio
  296. .SH "SEE ALSO"
  297. .IR fprintf (2),
  298. .IR fscanf (2),
  299. .IR fgetc (2)
  300. .br
  301. .IR open (2),
  302. .IR read (2)
  303. .SH DIAGNOSTICS
  304. The value
  305. .B EOF
  306. is returned uniformly to indicate that a
  307. .B FILE
  308. pointer has not been initialized with
  309. .IR fopen ,
  310. input (output) has been attempted on an output (input) stream,
  311. or a
  312. .B FILE
  313. pointer designates corrupt or otherwise unintelligible
  314. .B FILE
  315. data.
  316. .br
  317. Some of these functions set
  318. .IR errstr .
  319. .SH BUGS
  320. Buffering of output can prevent output data
  321. from being seen until long after it is computed \- perhaps
  322. never, as when an abort occurs between buffer filling and flushing.
  323. .br
  324. Buffering of input can cause a process to consume
  325. more input than it actually uses.
  326. This can cause trouble across
  327. .IR exec (2).
  328. .br
  329. Buffering may delay the receipt of a write error until a subsequent
  330. .I stdio
  331. writing, seeking, or file-closing call.
  332. .br
  333. ANSI says that a file can be fully buffered only if
  334. the file is not attached to an interactive device.
  335. In Plan 9 all are fully buffered except standard error.
  336. .PP
  337. .IR Fdopen ,
  338. .IR fileno ,
  339. .IR sopenr ,
  340. .IR sopenw ,
  341. and
  342. .I sclose
  343. are not ANSI Stdio functions.
  344. .PP
  345. Stdio offers no support for runes or
  346. .SM UTF
  347. characters.
  348. Unless external compatibility is necessary, use
  349. .IR bio (2),
  350. which supports
  351. .SM UTF
  352. and is smaller, faster, and simpler than Stdio.