I'm writing a program for class that reads from a file and then replaces a word with another word and writes that to a file. Right now I'm just doing the basic which is reading from one file and then writing to another....Which I thought would be pretty simple. I am familiar with C++ but with this I am confused on how to exactly do it.

The problem I'm having is that it will only write the last line and for some reason doesnt read the first lines.

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

#define MAX 512

int main(int argc, char *argv[])
{
  FILE *file, *file2;
  char line[MAX];

  if (argc != 4)
  {
    printf("You must enter: ./replace old-string new-string file-name\n");
    exit(1);
  }
  file = fopen(argv[3], "r");
  file2 = fopen("temp", "w");
  while (fgets(line,sizeof(line),file) != NULL);
  {
    /*Write the line */
    fputs(line, file2);
    printf(line);

  }
  fclose (file);
  fclose (file2);
  return 0;
}
(i'm using it in linux so...it would have to be like: ./replace string1 string2 FILENAME)

Thanks for any help you can give...or any direction you can point me in,

Dan