From owner-freebsd-questions Sun Mar 31 13: 4:57 2002 Delivered-To: freebsd-questions@freebsd.org Received: from tninet.se (lennier.tninet.se [195.100.94.105]) by hub.freebsd.org (Postfix) with ESMTP id 71B4837B400 for ; Sun, 31 Mar 2002 13:04:40 -0800 (PST) Received: from cs.umu.se (h16n1c1o1023.bredband.skanova.com [213.64.164.16]) by lennier.tninet.se (BMR ErlangTM/OTP 3.0) with ESMTP id 767580.608677.1017.1s17260936lennier ; Sun, 31 Mar 2002 23:04:37 +0200 Message-ID: <3CA779E5.F9C8DAFC@cs.umu.se> Date: Sun, 31 Mar 2002 23:04:37 +0200 From: Paul Everlund X-Mailer: Mozilla 4.77 [en] (Windows NT 5.0; U) X-Accept-Language: sv,en MIME-Version: 1.0 To: Eric Boucher , FreeBSD Subject: Re: exact length of a line in a file with the read command References: <20020331202713.66909.qmail@web9401.mail.yahoo.com> <3CA777FA.ED28BE07@cs.umu.se> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG Paul Everlund wrote: > > Eric Boucher wrote: > > > > Hi everyone, > > I have this problem: > > I want to know the exact length of each line in a > > file. Found a bug one minute after I did send my little program. :-) There should be an else statement in the code. This code below should work better (I hope). Probably there's more to fix, and if you get strange results, then let me know and I'll fix it. #include int main(int argc, char **argv) { FILE *f; char c; int row; int row_chars; if(argc != 2) { printf("Usage: %s count_chars_for_each_row_in_this_file\n", argv[0]); return 1; } if((f = fopen(argv[1], "r")) == NULL) { printf("File %s not found.\n", argv[1]); return 1; } row = 1; row_chars = 0; while((c = getc(f)) != EOF) { if(c == '\n') { printf("Row: %d / Number of chars: %d\n", row, row_chars); row++; row_chars = 0; } else row_chars++; } fclose(f); return 0; } Sorry about this, and good luck using it! Best regards, Paul To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message