C language read file

C language read file
/*
Example: read file into string in C language
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int main() {
    char filename[] = "test.txt";
    char * buffer = 0;
    long length;
    FILE * f = fopen (filename, "rb");

    if (f)
    {
      fseek (f, 0, SEEK_END);
      length = ftell (f);
      fseek (f, 0, SEEK_SET);
      buffer = malloc (length);
      if (buffer)
      {
        fread (buffer, 1, length, f);
      }
      fclose (f);
    }

    if (buffer)
    {
       // process data
       printf("%s",buffer);
    }

  return 0;
}
Source‮tual.www:‬turi.com
Created Time:2017-08-29 10:15:37  Author:lautturi