Command line arguments and ftruncate in C
<p>I am trying to make a C program that changes the size of input file to desired size by truncating or extending the input file using ftruncate. It must also use command line arguments.</p>

<p>For example, the following is valid input:</p>

<pre><code>./changefilesize data.txt 100
</code></pre>

<p>If the size of data.txt = 100, then return save file, if more than 100 then cut off the end, if less than 100, then extend the file size to 100.</p>

<p>I am having trouble dealing with input arguments and using ftruncate. The only information I found about ftruncate is basically the <code>man</code> info which says:</p>

<pre><code>#include &lt;unistd.h&gt;
int ftruncate(int fildes, off_t length);
int truncate(const char *path, off_t length);
</code></pre>

<p>Here is what I have so far:</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
int main (int argc, char *argv[]) {
    if ( argc != 2 ) {
        printf("2 inputs expected\n");
    }
    else {
        int length;
        length = atoi (argv[1]);
        FILE *file = fopen(argv[0], "r");
        int ftruncate(int file, off_t length);
        fclose(file);
    }
}
</code></pre>

<p>If I enter <code>./changefilesize data.txt 100</code>, I get <code>2 inputs expected</code> and I don't understand why.</p>

<p>Edit: updated code based on answers:</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
int main (int argc, char *argv[]) {
    long length;
    length = atoi (argv[2]);
    FILE *file = fopen(argv[1], "w");
    ftruncate (fileno(file), length);
    fclose(file);
}
</code></pre>