Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You have one of the closing parentheses in the wrong place:</p> <pre><code>(strcmp (argv[1], "-n" != 0)) </code></pre> <p>should be:</p> <pre><code>(strcmp (argv[1], "-n") != 0) </code></pre> <p>However, even once that's fixed, your argument processing is still not quite right.</p> <p>In <a href="https://stackoverflow.com/questions/12361558/c-basic-head-command">your previous question</a>, the <code>head -n COUNT FILE</code> was not a possibility, that made the argument checking reasonably easy.</p> <p>Here's the logic you need to follow now that you allow for a "floating" <code>-n count</code> section:</p> <pre><code>int linecount char *filename = NULL if argc == 2: linecount = 10 filename = argv[1] else: if argc == 4: if argv[1] == "-n": linecount = argv[2] filename = argv[3] else: if argv[2] == "-n": linecount = argv[3] filename = argv[1] if filename = NULL: generate some error </code></pre> <p>It basically catches the two-argument version first. If it's a four-argument version, it then finds out where the "-n" is so it can intelligently decide which argument is which value.</p> <p>As you can see, that's not quite what you have (your code looks for linecount in <code>argv[1]</code> which is <em>never</em> the case). You should use something like this as a guide:</p> <pre><code>argc argv[0] argv[1] argv[2] argv[3] ---- ------- ------- ------- ------- 2 head &lt;file&gt; 4 head &lt;file&gt; -n &lt;count&gt; 4 head -n &lt;count&gt; &lt;file&gt; </code></pre> <p>Once you refer to that, it should be easy to write the code for the different situations.</p> <p>You'll have to turn that pseudo-code of mine back into C of course (using <code>strcmp</code> instead of <code>==</code> for strings, and ensuring you use <code>atoi/strtol</code> for converting the string linecount argument to an integer), but that's the basic flow you should follow.</p>
 

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