title: Segmentation fault (core dumped) error, but program is working fine
body: <p>My objective is to print the non comment lines of a file to the screen or to a output file, however specified. My code is working fine in both cases (i.e. I am getting the desired output) However, when I print the lines to the screen, it works fine and after printing everything out, it says <code>Segmentation fault (core dumped)</code></p>
  
  <p><strong>Here is my code:</strong></p>
  
  <pre><code>/* Assignment 1 */
  
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  #include &lt;ctype.h&gt;
  
  #define MAXS 256
  
  int main(int argc, char *argv[])
  {
    FILE *fp;
    FILE *op;
    char line[MAXS] = {0};
    int max_x, max_y, num_pt, rand_inst;
    int ch;
    int inputfile = 0, outputfile = 0;
    int i;
  
  
    for (i = 1; i &lt; argc; i++)
      {
        if (strcmp (argv[i], "-i") == 0)
      inputfile = i+1;
        if (strcmp (argv[i], "-o") == 0)
      outputfile = i+1;
      }
    if (inputfile == 0)
      {
        /* invalid command line options */
        printf("\nIncorrect command-line...\n");
        printf("&gt; %s [-i inputfile [-o outputfile]]\n\n", argv[0]);
        exit(0);
      }
  
    fp = fopen(argv[inputfile], "r");
    if (fp == 0)
      {
        printf("\nCould not find %s\n", argv[inputfile]);
        exit(0);
      }
   /* while ((ch = fgetc(fp)) != EOF)) */
  
   /* This part is where I get the error but I get the desired output */
    if (outputfile == 0)
      {
        while (fgets (line, MAXS, fp) != NULL) 
          {         
            char *p = line;
            size_t len = strlen (line);
  
            while (len &gt; 0 &amp;&amp; (line[len-1] == '\n' || line[len-1] == '\r'))
                  line[--len] = 0;    /* strip newline or carriage rtn    */
  
  
            while (isspace (*p))  /*  advance to first non-whitespace  */
                  p++;
  
            /* skip lines beginning with '#' or blank lines  */
            if (*p == '#' || !*p)
                  continue;
  
            printf("%s\n", line);
  
          }
      }
    else
      {
        op = fopen(argv[outputfile], "w");
        while (fgets (line, MAXS, fp) != NULL) 
          {
            char *p = line;
  
            size_t len = strlen (line);
  
            while (len &gt; 0 &amp;&amp; (line[len-1] == '\n' || line[len-1] == '\r'))
                  line[--len] = 0;    /* strip newline or carriage rtn    */
  
  
            while (isspace (*p))  /*  advance to first non-whitespace  */
                  p++;
  
            /* skip lines beginning with '#' or blank lines  */
            if (*p == '#' || !*p)
                  continue;
  
            fprintf(op, "%s\n", line);
  
          }
      }
    fclose(op);
  
    return 0;
  }
  </code></pre>
  
  <p><strong>Here is the output which gives me segmentation fault:</strong></p>
  
  <blockquote>
    <p>100   100</p>
    
    <p>10</p>
    
    <p>0 0</p>
    
    <p>0 90</p>
    
    <p>70    100</p>
    
    <p>100   50</p>
    
    <p>30    30</p>
    
    <p>30    70</p>
    
    <p>70    70</p>
    
    <p>70    30</p>
    
    <p>50    50</p>
    
    <p>45    0</p>
    
    <p>Segmentation fault (core dumped)</p>
    
    <p>The above however, is the desired output minus the Segmentation fault error</p>
  </blockquote>
  
  <p><strong><em>I know Segmentation fault is something to do with reading/writing illegal memory location, but I cant quite figure out why the program is giving me the error provided I have no compilation errors and I am getting the desired output</em></strong></p>
  
  <p><strong>Language: c99 ; Compiler: gcc</strong></p>