title: How can i read from a file in C
body: <p>How do I read from a file using this function?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  char* read_from_file (const char* filename, size_t length)
  {
    //I have this so far
    fp = fopen(filename,"r"); // read mode
  
    if( fp == NULL )
    {
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }
  
    /* what goes in here? */
  
    return NULL;
  }
  </code></pre>
  
  <p>I'm trying to implement to the following guidelines:</p>
  
  <blockquote>
    <ul>
    <li><p>It allocates a char buffer of size length+1.</p></li>
    <li><p>It opens file filename, in read only mode. When it fails to open the file, it deallocates the buffer memory</p></li>
    <li><p>It reads length characters into the buffer, inserts a null character (\0) in the last position, closes the file and returns a pointer to the buffer. When it fails to read length characters, it deallocates the buffer memory, closes the file and returns NULL.</p></li>
    </ul>
  </blockquote>