string - Easiest way to get file's contents in C -


what simplest way (least error-prone, least lines of code, want interpret it) open file in c , read contents string (char*, char[], whatever)?

i tend load entire buffer raw memory chunk memory , parsing on own. way have best control on standard lib on multiple platforms.

this stub use this. may want check error-codes fseek, ftell , fread. (omitted clarity).

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) {   // start process data / extract strings here... } 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -