title: Segmentation fault in fgets() - C language
body: <p>I am getting a segmentation fault exactly at this line: </p>
  
  <pre><code>while (fgets(line, MAX_LEN + 1, stream) != NULL) {
  
  ....
  
  }
  </code></pre>
  
  <p>where MAX_LEN is 500, line is reading the current line, and stream is open through <code>fopen(filename, "r");</code></p>
  
  <p>I am reading lines from a file with a specific format and I get a segmentation fault (Core dumps) exactly at this line according to the debugger. </p>
  
  <p>I made my code so that it ignores lines that do not match the scanf format.</p>
  
  <p>Here is what I am implementing. Something close to it at least:</p>
  
  <pre><code>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 -&gt; %49[^@ ]@%49s", sender_username, sender_hostname,  receiver_username, receiver_hostname) != 4) {
    continue;
  } else {
  
  do work;
  
  }
  </code></pre>