Created
September 4, 2013 05:05
-
-
Save jeremyheiler/6432956 to your computer and use it in GitHub Desktop.
Print the contents of a file to stdout.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <errno.h> | |
int main(int argc, char **argv) | |
{ | |
if (argc == 1) return 0; | |
if (argc == 2) { | |
char *filename = argv[1]; | |
FILE *fp = fopen(filename, "r"); | |
if (!fp) { | |
perror(NULL); | |
return errno; | |
} | |
char buf[11]; | |
int read; | |
while ((read = fread(buf, sizeof(char), 10, fp)) != 0) { | |
buf[read] = '\0'; | |
printf(buf); | |
} | |
if (fp && fclose(fp) == EOF) { | |
perror(NULL); | |
return errno; | |
} | |
} | |
return 0; | |
} |
Oh, and printf
interprets that first arg as a format string, so if the file contains something like %d
, you are in funland. Either use fputs
or printf("%s", buf)
.
(Note that puts
appends a newline after each call, unlike fputs
, just to throw you. So puts(str)
is actually basically fputs(str, stdout); putchar('\n');
.)
Also: Once you have file reading, you can actually copy stdin to stdout by telling it to read from /dev/fd/0
or /dev/stdin
. Fake device files are fun! :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rather than nesting the entire body, you could test != 2 and bail early.
You probably want a non-zero exit code if there are no arguments. I'm used to sysexits.h defining EX_USAGE, but I don't know if anyone actually uses that but me.
fread
actually seems to return asize_t
; that's unsigned and could be (and likely is) larger thanint
, though I suppose that's not so much a worry with fixing the read size at 10 bytes. Still good practice to keep types and integer widths and signedness in mind.You're repeating the size of
buf
in a fragile way. Usesizeof(buf) - 1
instead of 10.sizeof(char)
is also guaranteed to be 1, but it'd be clearer to just dosizeof(*buf)
, since that's really what you're reading: "fill up buf less 1 with the stuff that buf holds". Doing this completely independently of the size of*buf
would require you to read(sizeof(buf)/sizeof(*buf) - 1) * sizeof(*buf)
items, which admittedly is a bit involved. :)You're not testing whether that
0
return fromfread
is due to EOF or an actual error. Manpage advisesfeof
andferror
to distinguish the cause.<stdlib.h>
actually declaresEXIT_SUCCESS
andEXIT_FAILURE
, which I think could theoretically be defined to values other than 0 and 1.