Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

154 linhas
7.0KB

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <title>fopen (The Red Hat newlib C Library)</title>
  7. <meta name="description" content="fopen (The Red Hat newlib C Library)">
  8. <meta name="keywords" content="fopen (The Red Hat newlib C Library)">
  9. <meta name="resource-type" content="document">
  10. <meta name="distribution" content="global">
  11. <meta name="Generator" content="makeinfo">
  12. <link href="index.html#Top" rel="start" title="Top">
  13. <link href="Document-Index.html#Document-Index" rel="index" title="Document Index">
  14. <link href="Document-Index.html#SEC_Contents" rel="contents" title="Table of Contents">
  15. <link href="Stdio.html#Stdio" rel="up" title="Stdio">
  16. <link href="fopencookie.html#fopencookie" rel="next" title="fopencookie">
  17. <link href="fmemopen.html#fmemopen" rel="prev" title="fmemopen">
  18. <style type="text/css">
  19. <!--
  20. a.summary-letter {text-decoration: none}
  21. blockquote.indentedblock {margin-right: 0em}
  22. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  23. blockquote.smallquotation {font-size: smaller}
  24. div.display {margin-left: 3.2em}
  25. div.example {margin-left: 3.2em}
  26. div.lisp {margin-left: 3.2em}
  27. div.smalldisplay {margin-left: 3.2em}
  28. div.smallexample {margin-left: 3.2em}
  29. div.smalllisp {margin-left: 3.2em}
  30. kbd {font-style: oblique}
  31. pre.display {font-family: inherit}
  32. pre.format {font-family: inherit}
  33. pre.menu-comment {font-family: serif}
  34. pre.menu-preformatted {font-family: serif}
  35. pre.smalldisplay {font-family: inherit; font-size: smaller}
  36. pre.smallexample {font-size: smaller}
  37. pre.smallformat {font-family: inherit; font-size: smaller}
  38. pre.smalllisp {font-size: smaller}
  39. span.nolinebreak {white-space: nowrap}
  40. span.roman {font-family: initial; font-weight: normal}
  41. span.sansserif {font-family: sans-serif; font-weight: normal}
  42. ul.no-bullet {list-style: none}
  43. -->
  44. </style>
  45. </head>
  46. <body lang="en">
  47. <a name="fopen"></a>
  48. <div class="header">
  49. <p>
  50. Next: <a href="fopencookie.html#fopencookie" accesskey="n" rel="next">fopencookie</a>, Previous: <a href="fmemopen.html#fmemopen" accesskey="p" rel="prev">fmemopen</a>, Up: <a href="Stdio.html#Stdio" accesskey="u" rel="up">Stdio</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
  51. </div>
  52. <hr>
  53. <a name="fopen_002d_002d_002dopen-a-file"></a>
  54. <h3 class="section">4.17 <code>fopen</code>&mdash;open a file</h3>
  55. <a name="index-fopen"></a>
  56. <a name="index-_005ffopen_005fr"></a>
  57. <p><strong>Synopsis</strong>
  58. </p><div class="example">
  59. <pre class="example">#include &lt;stdio.h&gt;
  60. FILE *fopen(const char *<var>file</var>, const char *<var>mode</var>);
  61. FILE *_fopen_r(struct _reent *<var>reent</var>,
  62. const char *<var>file</var>, const char *<var>mode</var>);
  63. </pre></div>
  64. <p><strong>Description</strong><br>
  65. <code>fopen</code> initializes the data structures needed to read or write a
  66. file. Specify the file&rsquo;s name as the string at <var>file</var>, and the kind
  67. of access you need to the file with the string at <var>mode</var>.
  68. </p>
  69. <p>The alternate function <code>_fopen_r</code> is a reentrant version.
  70. The extra argument <var>reent</var> is a pointer to a reentrancy structure.
  71. </p>
  72. <p>Three fundamental kinds of access are available: read, write, and append.
  73. <code>*<var>mode</var></code> must begin with one of the three characters &lsquo;<code>r</code>&rsquo;,
  74. &lsquo;<code>w</code>&rsquo;, or &lsquo;<code>a</code>&rsquo;, to select one of these:
  75. </p>
  76. <dl compact="compact">
  77. <dt><code>r</code></dt>
  78. <dd><p>Open the file for reading; the operation will fail if the file does
  79. not exist, or if the host system does not permit you to read it.
  80. </p>
  81. </dd>
  82. <dt><code>w</code></dt>
  83. <dd><p>Open the file for writing <em>from the beginning</em> of the file:
  84. effectively, this always creates a new file. If the file whose name you
  85. specified already existed, its old contents are discarded.
  86. </p>
  87. </dd>
  88. <dt><code>a</code></dt>
  89. <dd><p>Open the file for appending data, that is writing from the end of
  90. file. When you open a file this way, all data always goes to the
  91. current end of file; you cannot change this using <code>fseek</code>.
  92. </p></dd>
  93. </dl>
  94. <p>Some host systems distinguish between &ldquo;binary&rdquo; and &ldquo;text&rdquo; files.
  95. Such systems may perform data transformations on data written to, or
  96. read from, files opened as &ldquo;text&rdquo;.
  97. If your system is one of these, then you can append a &lsquo;<code>b</code>&rsquo; to any
  98. of the three modes above, to specify that you are opening the file as
  99. a binary file (the default is to open the file as a text file).
  100. </p>
  101. <p>&lsquo;<code>rb</code>&rsquo;, then, means &ldquo;read binary&rdquo;; &lsquo;<code>wb</code>&rsquo;, &ldquo;write binary&rdquo;; and
  102. &lsquo;<code>ab</code>&rsquo;, &ldquo;append binary&rdquo;.
  103. </p>
  104. <p>To make C programs more portable, the &lsquo;<code>b</code>&rsquo; is accepted on all
  105. systems, whether or not it makes a difference.
  106. </p>
  107. <p>Finally, you might need to both read and write from the same file.
  108. You can also append a &lsquo;<code>+</code>&rsquo; to any of the three modes, to permit
  109. this. (If you want to append both &lsquo;<code>b</code>&rsquo; and &lsquo;<code>+</code>&rsquo;, you can do it
  110. in either order: for example, <code>&quot;rb+&quot;</code> means the same thing as
  111. <code>&quot;r+b&quot;</code> when used as a mode string.)
  112. </p>
  113. <p>Use <code>&quot;r+&quot;</code> (or <code>&quot;rb+&quot;</code>) to permit reading and writing anywhere in
  114. an existing file, without discarding any data; <code>&quot;w+&quot;</code> (or <code>&quot;wb+&quot;</code>)
  115. to create a new file (or begin by discarding all data from an old one)
  116. that permits reading and writing anywhere in it; and <code>&quot;a+&quot;</code> (or
  117. <code>&quot;ab+&quot;</code>) to permit reading anywhere in an existing file, but writing
  118. only at the end.
  119. </p>
  120. <br>
  121. <p><strong>Returns</strong><br>
  122. <code>fopen</code> returns a file pointer which you can use for other file
  123. operations, unless the file you requested could not be opened; in that
  124. situation, the result is <code>NULL</code>. If the reason for failure was an
  125. invalid string at <var>mode</var>, <code>errno</code> is set to <code>EINVAL</code>.
  126. </p>
  127. <br>
  128. <p><strong>Portability</strong><br>
  129. <code>fopen</code> is required by ANSI C.
  130. </p>
  131. <p>Supporting OS subroutines required: <code>close</code>, <code>fstat</code>, <code>isatty</code>,
  132. <code>lseek</code>, <code>open</code>, <code>read</code>, <code>sbrk</code>, <code>write</code>.
  133. </p>
  134. <br>
  135. <hr>
  136. <div class="header">
  137. <p>
  138. Next: <a href="fopencookie.html#fopencookie" accesskey="n" rel="next">fopencookie</a>, Previous: <a href="fmemopen.html#fmemopen" accesskey="p" rel="prev">fmemopen</a>, Up: <a href="Stdio.html#Stdio" accesskey="u" rel="up">Stdio</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
  139. </div>
  140. </body>
  141. </html>