Write a program that sequentially displays on screen all the files listed in the command line. - C Function

C examples for Function:main function

Description

Write a program that sequentially displays on screen all the files listed in the command line.

Demo Code

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

int main(int argc, char *argv[])
{
  FILE * fp;//from w  w w .  ja va  2s.  c om
  int ch;

  if (argc == 1)
  {
    printf("Usage: %s file1 file2 ...\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  for (int i = 1; i < argc; i++)
  {
    if ((fp = fopen(argv[i], "r")) == NULL)
    {
      fprintf(stderr, "Could not open file %s.\n", argv[i]);
      exit(EXIT_FAILURE);
    }

    while ((ch = getc(fp)) != EOF)
      putc(ch, stdout);

    fclose(fp);
  }

  return 0;
}

Related Tutorials