From b094f6f873423c7f14118ae92005451b7a7d75bb Mon Sep 17 00:00:00 2001 From: Maxime Alvarez Date: Sun, 11 Mar 2018 00:26:06 +0100 Subject: [PATCH] Improvements and reading from stdin - Replaced 4 consecutives calls to putc by 1 call to puts - Added a parameter check to see if a file has been given as input (if no, the standard input is used) --- tabs.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/tabs.c b/tabs.c index c9c5e1c..b78adbd 100644 --- a/tabs.c +++ b/tabs.c @@ -2,8 +2,6 @@ /* TODO - - improve the repeated putchar(' '); - - use stdin instead of a file for input - error handling... PULL REQUESTS ARE WELCOME! @@ -11,22 +9,25 @@ #include -int main() { - File *fp; +int main(int argc, char *argv[]) { + File *in = stdin; int c; - fp = fopen("input.txt", "R"); + + if(argc == 2) { + fp = fopen("input.txt", "R"); + + if(fp != NULL) + in = fp; + } while ((c = fgetc(fp)) != EOF) { if (c == '\t') { - putchar(' '); - putchar(' '); - putchar(' '); - putchar(' '); + puts(" "); } else { putchar(c); } } - fclose(fp); + fclose(in); return 0; }