#include <stdio.h>
#include <stdlib.h>

int read_string(FILE * file, char *buff, int maxsz)
{
  int i;
  int ch;
  int len = 0;

  /* try looping for maxsz characters */
  for(i=0;i<maxsz;i++)
  {
    /* read character */
    ch = fgetc(file);

    /* eof ? */
    if(ch == -1) 
    {
      /* if was 1st read, say is eof */
      if(len == 0) return -1;

      /* otherwise have read other stuff already
         so break so we return what has been read */
      break;
    }
    /* end of string - stop loop */
    if(ch == '\n') break;  
 
    /* not end of string, so store character */
    buff[len] = ch;       

    /* inc character count */
    len ++;
  }

  /* null terminate the string */
  buff[len] = '\0';

  /* return num of characters in string */
  return len;
}



int main(int argc, char **argv)
{
  char buffer[512+1];
  int line = 0;

  while( read_string(stdin, buffer, 512) != -1)
  {
    printf("%6d %s\n", line, buffer); 
    line ++;
  } 
  return 0;
}
