From 17a506ed4e3f3c0ee89257549d2b4f0ea82cb899 Mon Sep 17 00:00:00 2001 From: Calvin Morrison Date: Fri, 30 Aug 2013 14:15:26 -0400 Subject: proper check, and use fgest instead of getc --- count_nucleobases.c | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/count_nucleobases.c b/count_nucleobases.c index 2f3dce8..0396ea6 100755 --- a/count_nucleobases.c +++ b/count_nucleobases.c @@ -16,29 +16,34 @@ int main(int argc, char **argv) { } FILE *fh = fopen(argv[1], "r" ); if(fh == NULL) { - fprintf(stderr, "could not open dna.txt\n"); + fprintf(stderr, "Couldn't open: %s\n", argv[1]); exit(EXIT_FAILURE); } - int ch = 0; - while ((ch = getc(fh)) != EOF) { - switch(ch) { - case 'A': - case 'a': - a++; - break; - case 'C': - case 'c': - c++; - break; - case 'G': - case 'g': - g++; - break; - case 'T': - case 't': - t++; - break; + char line[8192]; + while (fgets(line, 8192, fh) != NULL) { + + int i = 0; + + for(i = 0; i < strlen(line); i++) { + switch(line[i]) { + case 'A': + case 'a': + a++; + break; + case 'C': + case 'c': + c++; + break; + case 'G': + case 'g': + g++; + break; + case 'T': + case 't': + t++; + break; + } } } -- cgit v1.2.3