Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 31 Mar 2002 23:04:37 +0200
From:      Paul Everlund <tdv94ped@cs.umu.se>
To:        Eric Boucher <eric_boucher60@yahoo.com>, FreeBSD <freebsd-questions@FreeBSD.ORG>
Subject:   Re: exact length of a line in a file with the read command
Message-ID:  <3CA779E5.F9C8DAFC@cs.umu.se>
References:  <20020331202713.66909.qmail@web9401.mail.yahoo.com> <3CA777FA.ED28BE07@cs.umu.se>

next in thread | previous in thread | raw e-mail | index | archive | help
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 <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;
    }
    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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?3CA779E5.F9C8DAFC>