From owner-freebsd-questions Sun Mar 31 12:56:36 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 14EEC37B417 for ; Sun, 31 Mar 2002 12:56:30 -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 497346.608186.1017.0s16677130lennier ; Sun, 31 Mar 2002 22:56:26 +0200 Message-ID: <3CA777FA.ED28BE07@cs.umu.se> Date: Sun, 31 Mar 2002 22:56:26 +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 Cc: FreeBSD Subject: Re: exact length of a line in a file with the read command References: <20020331202713.66909.qmail@web9401.mail.yahoo.com> 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 Eric Boucher wrote: > > Hi everyone, > I have this problem: > I want to know the exact length of each line in a > file. But it seems that when the file begin or end > with a space caracter of a tab caracter, the "read" > command in bourne shell delete them and return me only > what begin with a letter and end with a letter. So > when I try to know the exact length of a file (I tried > with both lenght command from "expr" and "nawk"). So > if for example, a line is like this (note: > mean the real space caracter and the tab > caracter) > toto > the read return me only: > toto > which is of length = 4 > but it is suppose to be of length = 6 > > The same thing append if the space or the tab caracter > are at the end of a line. > I also tried to make IFS="", so the there is no field > seperator, but it doesn't seems to work too. > Can somebody help me? > Thanks Copy this little program into an ordinary text file: #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; } row_chars++; } fclose(f); return 0; } Then save it as 'name.c', where name is the name you want the program to have, for example: count_chars.c Then type the following: # cc -o name name.c Now you should be able to write at the prompt: # ./name file File is the text file where you want to count the chars for each row. Maybe too much for solving the problem, but it worked right now when I did test it. Use it on your own risk though. :-) Best regards, Paul To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message