Logo  

CS256 - Fundamentals of Structured Programming

Displaying ./code/files/cat3.c

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

#define K	1000

int my_strlen(char *str)
{
  int len = 0;
  while(str[len] != '\0') len++;
  return len;
}

int main(int argc, char *argv[])
{
  if (argc < 2) {
    printf("Usage: ./cat <filename>\n");
    return 0;
  }

  FILE *fp = fopen(argv[1], "r");
  if (fp == NULL) {
    printf("Error opening file for input.\n");
    exit(1);
  }

  char buf[K];
  while ( fgets(buf, K, fp) != NULL ) {
    printf("%d\n", my_strlen(buf));
  }

  fclose(fp);

  return 0;
}