(ReadShader)Simple File Reading Code in C++ with File Size buffer
//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.
#ifndef READSHADER_H
#define READSHADER_H
#include
#include
using namespace std;
char * readShader(char *fileName){
cout <<”nothing”;
ifstream input(fileName);
if(!input){
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);
input.read(data,fileSize);
input.close();
data[fileSize]= ”; //appending the zero as the read does not append that
return data;
};
#endif