Date: Sun, 31 Mar 2002 22:56:26 +0200 From: Paul Everlund <tdv94ped@cs.umu.se> To: Eric Boucher <eric_boucher60@yahoo.com> Cc: FreeBSD <freebsd-questions@FreeBSD.ORG> Subject: Re: exact length of a line in a file with the read command Message-ID: <3CA777FA.ED28BE07@cs.umu.se> References: <20020331202713.66909.qmail@web9401.mail.yahoo.com>
next in thread | previous in thread | raw e-mail | index | archive | help
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: <space> > mean the real space caracter and <tab> the tab > caracter) > <space>toto<tab> > 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 <stdio.h> 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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3CA777FA.ED28BE07>