title: Reading a file using fgets and printing its content on screen
body: <p>I still consider myself as a new webby in C.I am trying to read a file in, file is not binary but its size varies from small size of feb kbs to big size files of feb Mbs.I am using fgets function, i had taken <a href="http://publib.boulder.ibm.com/infocenter/zvm/v6r1/index.jsp?topic=/com.ibm.zvm.v610.edclv/fgets.htm" rel="nofollow noreferrer">reference from this link</a> but after compilation, i am getting segmentation fault.i tried to debug it with gdb and found that i am able to open file but unable to read.Here is my code.</p>
  
  <pre><code>    #include&lt;stdio.h&gt;
      #define MAX_LENGTH 1048576
      int main()
      {
      FILE *fp;
      char *result;
      char line[MAX_LENGTH];
      fp =fopen("/home/shailendra/sampleprograms/C/shail1.txt","r");
      if(result=fgets(line,MAX_LENGTH,fp) != NULL)
      printf("The string is %s \n",result);
  
      else
      printf("Error opening the file");
  
      if(fclose(fp))
      printf("fclose error");
      }
  </code></pre>
  
  <p>This Segmentation fault really sucks your blood.I understand that it is due to insufficient memory allocation but i had used MAX_LENGTH 1048576, so i don't think that it must create any problem.I had tried it with both small files with only one line and a big file with multiple lines but i am unable to figure out why i am getting segmentation fault.</p>
  
  <p>I also looked at <a href="https://stackoverflow.com/questions/13498233/effective-methods-for-reading-and-writing-large-files-in-c">this</a> and <a href="https://stackoverflow.com/questions/11790822/reading-a-large-file-using-c-greater-than-4gb-using-read-function-causing-pro">this</a> but got no help.</p>