From 5cac5b2139441a8a333a7128043bced3f27e627f Mon Sep 17 00:00:00 2001 From: Kimmo Kulovesi Date: Sun, 29 Dec 2013 19:17:49 +0200 Subject: [PATCH] Correctly count lines --- ihex2bin.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ihex2bin.c b/ihex2bin.c index 7612b2b..6724823 100644 --- a/ihex2bin.c +++ b/ihex2bin.c @@ -12,6 +12,7 @@ #include #include #include +#include static FILE *outfile; static unsigned long line_number = 1; @@ -21,7 +22,7 @@ int main (int argc, char *argv[]) { struct ihex_state ihex; unsigned int count; - char buf[1024]; + char buf[256]; if (argc == 2) { if (!(outfile = fopen(argv[1], "wb"))) { @@ -33,11 +34,10 @@ main (int argc, char *argv[]) { } ihex_begin_read(&ihex); - while ((count = fread(buf, 1, sizeof(buf), stdin))) { + while (fgets(buf, sizeof(buf), stdin)) { + count = strlen(buf); + line_number += (count && buf[count - 1] == '\n') ? 1 : 0; ihex_read_bytes(&ihex, buf, count); - do { - line_number += (buf[--count] == '\n') ? 1 : 0; - } while (count); } ihex_end_read(&ihex);