Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 48 lines (39 sloc) 0.928 kb
dc5be1c Initial check-in.
uli authored
1 /*
2 * GetFileContents.c
3 * Carlson
4 *
5 * Created by Uli Kusterer on 16.04.06.
6 * Copyright 2006 Uli Kusterer. All rights reserved.
7 *
8 */
9
10 #include "GetFileContents.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13
14
15 char* GetFileContents( const char* fname )
16 {
17 // Open script to run:
18 FILE* theFile = fopen( fname, "r" );
19 if( !theFile )
20 {
21 printf("ERROR: Can't open file \"%s\".\n", fname);
22 return NULL;
23 }
24
25 // Find out file length:
26 fseek( theFile, 0, SEEK_END );
27 int len = ftell( theFile ),
28 readbytes;
29 char* codeStr = (char*) malloc( len +1 );
30
31 // Rewind and read in whole file:
32 fseek( theFile, 0, SEEK_SET );
33 readbytes = fread( codeStr, 1, len, theFile );
34 if( readbytes != len )
35 {
36 free( codeStr );
37 fclose( theFile );
38 printf("ERROR: Couldn't read from file \"%s\" (%d bytes read).\n",fname,readbytes);
39 return NULL;
40 }
41 codeStr[len] = 0; // Terminate string.
42 fclose( theFile );
43
44 return codeStr;
45 }
46
47
Something went wrong with that request. Please try again.