Here is a more complete example with some patterns that you should study and may want to reuse. It is our familiar friend that copies an input file to an output file line by line. But getLine has been generalized to newLine. Instead of passing it a buffer to fill in with a string, newLine creates a new string object containing the contents of the line it gets.

It also illustrates the use of command line parameters and files, which you met briefly in the last lab.

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

/* Read a line of text into line buffer
   clean up potential missing lf in final line
 */
#define MAXLINE 128
char *newLine(FILE *infile) {
  int i, ch;
  char *s = NULL;
  char line[MAXLINE];		/* Temporary buffer */
  int len = 0;
  if (feof(infile)) return NULL;
  /* Read the line into a local buffer upto newline */
  ch = fgetc(infile);
  while ((ch != EOF) && (ch != '\n')) {
    if (len < sizeof(line)-1) line[len++] = ch;
    ch = fgetc(infile);
  }
  /* Allocate a string to hold it, copy and return */
  s = malloc(len+1);
  for (i = 0; i < len; i++) s[i] = line[i];
  s[i] = '\0';
  return s;
}

int main(int argc, char *argv[]) {
  char *s;
  FILE *infile;
  FILE *ofile;

  infile = stdin;
  ofile = stdout;
  if (argc >= 3) ofile  = fopen(argv[2],"w");
  if (argc >= 2) infile = fopen(argv[1],"r");

  s = newLine(infile);
  while (s) {
    fprintf(ofile,"%s\n",s);
    s = newLine(infile);
  }
  fclose(ofile);
  return 0;
}