Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are several problems with your code.</p> <p><code>int main()</code> should be <code>int main(void)</code>; this is a minor issue that almost certainly won't hurt anything, but the latter is more correct.</p> <p>You don't check whether the <code>fopen()</code> calls succeed.</p> <p>You're using <code>i</code> to count the characters you read, but you never do anything with its value.</p> <p>The <code>getc()</code> function returns a result of type <code>int</code>, so you should <em>definitely</em> make <code>tempChar</code> an <code>int</code>. The reason for this is that it can return either a valid character value (which will fit in a <code>char</code> object) <em>or</em> the value <code>EOF</code> which is typically <code>-1</code>. By storing the result of <code>getc()</code> in a <code>char</code> object, either you'll never see <code>EOF</code> (if plain <code>char</code> is unsigned), or you won't be able to distinguish <code>EOF</code> from a valid input character.</p> <p>In a comment on Razvan's answer, you said you changed the test to <code>tempChar != EOF</code>. Apart from the problem I explained above, on the first iteration of the loop <code>tempChar</code> has not been initialized, and the result of the comparison is unpredictable.</p> <p>The conventional way to write an input loop using <code>getc()</code> is:</p> <pre><code>int c; while ((c = getc(f)) != EOF) { /* do something with c */ } </code></pre> <p>As a matter of style, <code>write</code> is not a very good name for a <code>FILE*</code>. For one thing, there's a function of that name (defined by POSIX, not by C, but it's still potentially confusing). You might call the <code>FILE*</code> objects <code>in</code> and <code>out</code> instead.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload