Mercurial > hg > Members > nobuyasu > CbC
changeset 17:2ec14e6d8303 draft
modify Huffman
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 31 Jul 2012 04:55:48 +0900 |
parents | f1ffbc100990 |
children | f426762609d4 |
files | Huffman/Makefile Huffman/test-huffman.c Huffman/test.txt |
diffstat | 3 files changed, 23 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/Huffman/Makefile Tue Jul 31 04:18:20 2012 +0900 +++ b/Huffman/Makefile Tue Jul 31 04:55:48 2012 +0900 @@ -1,5 +1,6 @@ CC = gcc -CFLAGS = -O3 +#CFLAGS = -O3 +CFLAGS = -O0 -g3 PROG = test-huffman all: $(PROG)
--- a/Huffman/test-huffman.c Tue Jul 31 04:18:20 2012 +0900 +++ b/Huffman/test-huffman.c Tue Jul 31 04:55:48 2012 +0900 @@ -5,7 +5,9 @@ -#define BUF 1024 +#define READ_BUF_SIZE 1024 + +#define INPUT(fp, buf, result, max_size) result = fread(buf, 1, max_size, fp) int main(int argc, char* argv[]) { @@ -13,22 +15,27 @@ FILE *fp; char *filename = "./test.txt"; if ( (fp = fopen( filename,"r")) == NULL ) { - fprintf(stderr, "can't open file %s\n", filename); + fprintf(stderr, "Can't open file %s\n", filename); exit(0); } - char c; - while (c = getc(fp)) { - if (feof(fp)) { + char base[READ_BUF_SIZE]; + char *buf; + size_t n_chars; + INPUT(fp, base, n_chars, READ_BUF_SIZE); + + do { + buf = base; + int ret; + while ( (ret = (int)*buf) != '\0' ) { + putc(*buf,stdout); + ++buf; + } + if ( n_chars < READ_BUF_SIZE ) { break; } - - putc(c,stdout); + } while ( INPUT(fp, base, n_chars, READ_BUF_SIZE)); - } - - - - + fclose(fp); return 0; }