Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>#include &lt;stdio.h&gt; static void cat(FILE *fp) { char buffer[4096]; size_t nbytes; while ((nbytes = fread(buffer, sizeof(char), sizeof(buffer), fp)) != 0) fwrite(buffer, sizeof(char), nbytes, stdout); } int main(int argc, char **argv) { FILE *fp; const char *file; while ((file = *++argv) != 0) { if ((fp = fopen(file, "r")) != 0) { cat(fp); fclose(fp); } } return(0); } </code></pre> <p>The <code>cat()</code> function is not strictly necessary, but I'd rather use it. The main program steps through each command line argument and opens the named file. If it succeeds, it calls the <code>cat()</code> function to print its contents. Since the call to <code>fopen()</code> does not specify <code>"rb"</code>, it is opened as a text file. If the file is not opened, this code silently ignores the issue. If no files are specified, nothing is printed at all.</p> <p>The <code>cat()</code> function simply reads blocks of text up to 4096 bytes at a time, and writes them to standard output ('the screen'). It stops when there's no more to read.</p> <p>If you want to extend the code to read standard input when no file is specified, then you can use:</p> <pre><code>if (argc == 1) cat(stdin); else { ...while loop as now... } </code></pre> <p>which is one of the reasons for having the <code>cat()</code> function written as shown.</p> <p>This code does not pay direct attention to newlines — or lines of any sort. If you want to process it formally one line at a time, then you can do several things:</p> <pre><code>static void cat(FILE *fp) { char buffer[4096]; while (fgets(buffer, sizeof(buffer), fp) != 0) fputs(buffer, stdout); } </code></pre> <p>This will read and write one line at a time. If any line is longer than 4095 bytes, it will read the line in two or more operations and write it in the same number of operations. Note that this assumes a text file in a way that the version using <code>fread()</code> and <code>fwrite()</code> does not. On POSIX systems, the version with <code>fread()</code> and <code>fwrite()</code> will handle arbitrary binary files with null bytes (<code>'\0'</code>) in the data, but the version using <code>fgets()</code> and <code>fputs()</code> will not. Both the versions so far are strictly standard C (any version of the standard) as they don't use any platform-specific extensions; they are about as portable as code can be.</p> <p>Alternatively again, if you have the POSIX 2008 <a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html" rel="nofollow"><code>getline()</code></a> function, you can use that, but you need <code>#include &lt;stdlib.h&gt;</code> too (because you end up having to release the memory it allocates):</p> <pre><code>static void cat(FILE *fp) { char *buffer = 0; size_t buflen = 0; while (getline(&amp;buffer, &amp;buflen, fp) != -1) fputs(buffer, stdout); free(buffer); } </code></pre> <p>This version, too, will not handle binary data (meaning data with null bytes in it). It could be upgraded to do so, of course:</p> <pre><code>static void cat(FILE *fp) { char *buffer = 0; size_t buflen = 0; ssize_t nbytes; while ((nbytes = getline(&amp;buffer, &amp;buflen, fp)) != -1) fwrite(buffer, sizeof(char), nbytes, stdout); free(buffer); } </code></pre> <p>The <code>getline()</code> function reports how many bytes it read (there's a null byte after that), but the <code>fwrite()</code> function is the only one that takes a stream of arbitrary bytes and writes them all to the given stream.</p>
    singulars
    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.
 

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