Błąd segmentacji w języku fgets () - C

Dostaję błąd segmentacji dokładnie w tej linii:

while (fgets(line, MAX_LEN + 1, stream) != NULL) {

....

}

gdzie MAX_LEN ma wartość 500, linia odczytuje bieżącą linię, a strumień jest otwartyfopen(filename, "r");

Czytam linie z pliku o określonym formacie i otrzymuję błąd segmentacji (zrzuty Core) dokładnie na tej linii zgodnie z debuggerem.

Zrobiłem mój kod, aby ignorował wiersze, które nie pasują do formatu scanf.

Oto, co wdrażam. Coś blisko tego:

int main(int argc, int **argv) {
....
....
if (argc == 1) {
printf("enter file name: ");
scanf("%s", filename);
if (!(stream = fopen(filename, "r"))) {
    perror("Error message goes here.");
    exit(EXIT_FAILURE);
}


} else if (argc == 2) {
stream = fopen(argv[1], "r");
 if (sream == NULL) {
   fprintf(stderr, "error message", argv[1], errno));
   return EX_OSERR;
 }
}

while (fgets(line, MAX_LEN + 1, stream) != NULL) {
 if (sscanf(line, "%49[^@ ]@%49s -> %49[^@ ]@%49s", sender_username, sender_hostname,  receiver_username, receiver_hostname) != 4) {
  continue;
} else {

do work;

}

questionAnswers(0)

yourAnswerToTheQuestion