 <p>Based on NilObject's code:</p> <pre><code>#include &lt;sys/stat.h&gt; off_t fsize(const char *filename) { struct stat st; if (stat(filename, &amp;st) == 0) return st.st_size; return -1; } </code></pre> <p>Changes:</p> <ul> <li>Made the filename argument a <code>const char</code>.</li> <li>Corrected the <code>struct stat</code> definition, which was missing the variable name.</li> <li>Returns <code>-1</code> on error instead of <code>0</code>, which would be ambiguous for an empty file. <code>off_t</code> is a signed type so this is possible.</li> </ul> <p>If you want <code>fsize()</code> to print a message on error, you can use this:</p> <pre><code>#include &lt;sys/stat.h&gt; #include &lt;string.h&gt; #include &lt;stdio.h&gt; #include &lt;errno.h&gt; off_t fsize(const char *filename) { struct stat st; if (stat(filename, &amp;st) == 0) return st.st_size; fprintf(stderr, "Cannot determine size of %s: %s\n", filename, strerror(errno)); return -1; } </code></pre> <p>On 32-bit systems you should compile this with the option <code>-D_FILE_OFFSET_BITS=64</code>, otherwise <code>off_t</code> will only hold values up to 2 GB. See the "Using LFS" section of <a href="http://www.suse.de/~aj/linux_lfs.html" rel="nofollow noreferrer">Large File Support in Linux</a> for details.</p>