Posts Tagged ‘Loading Shaders’

(ReadShader)Simple File Reading Code in C++ with File Size buffer

April 5, 2008 Leave a comment

//impressed by lighthouse 3d and help taken from sources on the internet.
//this is not my idea, not my full code.

//It reads the full file in single go.

using namespace std;

char * readShader(char *fileName){

cout <<“nothing”;
ifstream input(fileName);
cout << endl << “UNABLE TO OPEN FILE ” << fileName <<endl;
return 0;

int st = input.tellg();
input.seekg(0, ios::end);
int end = input.tellg();
int fileSize = end – st;
if(fileSize <= 0){return 0;}

char *data = new char[fileSize+50];//50 added to just have buffer
memset(data, ”,fileSize+10);
input.seekg(0, ios::beg);,fileSize);
data[fileSize]= ”; //appending the zero as the read does not append that
return data;