From c2dfccab8aa22f308dc572b18658036e9db01107 Mon Sep 17 00:00:00 2001 From: orbitz Date: Sun, 29 Dec 2013 12:36:37 +0100 Subject: Refactor to pull opening the file out of the count function --- kmer_total_count.c | 9 ++++++++- kmer_utils.c | 8 +------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/kmer_total_count.c b/kmer_total_count.c index f15b69c..5ce7ee1 100644 --- a/kmer_total_count.c +++ b/kmer_total_count.c @@ -12,6 +12,7 @@ int main(int argc, char **argv) { char *filename = NULL; + FILE *fh; unsigned int kmer = 0; @@ -76,9 +77,15 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } + fh = fopen(filename, "r"); + if(fh == NULL) { + fprintf(stderr, "Could not open %s\n", filename); + exit(EXIT_FAILURE); + } + width = pow_four(kmer); - unsigned long long *counts = get_kmer_counts_from_file(filename, kmer); + unsigned long long *counts = get_kmer_counts_from_file(fh, kmer); // If nonzero is set, only print non zeros if(nonzero) { diff --git a/kmer_utils.c b/kmer_utils.c index e818a27..9e4bdd8 100644 --- a/kmer_utils.c +++ b/kmer_utils.c @@ -107,7 +107,7 @@ char *strnstrip(const char *s, char *dest, int c, unsigned long long len) { return dest; } -unsigned long long * get_kmer_counts_from_file(const char *fn, const unsigned int kmer) { +unsigned long long * get_kmer_counts_from_file(FILE *fh, const unsigned int kmer) { char *line = NULL; size_t len = 0; ssize_t read; @@ -115,12 +115,6 @@ unsigned long long * get_kmer_counts_from_file(const char *fn, const unsigned in long long i = 0; long long position = 0; - FILE * const fh = fopen(fn, "r"); - if(fh == NULL) { - fprintf(stderr, "Error opening %s - %s\n", fn, strerror(errno)); - exit(EXIT_FAILURE); - } - // width is 4^kmer // there's a sneaky bitshift to avoid pow dependency const unsigned long width = pow_four(kmer); -- cgit v1.2.1